一、选择法排序
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int s[2000], n;
int i, j;
cin >> n;
for ( i = 0 ; i < n ; i++ )
cin >> s[i];
for ( i = 0 ; i < n - 1 ; i++ )
for ( j = i + 1 ; j < n ; j++ )
if ( s[i] > s[j] )
swap( s[i] , s[j] );
cout << s[0];
for ( i = 1 ; i < n ; i++ )
cout << " " << s[i];
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
二、冒泡法
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int s[2000], n;
int i, j;
cin >> n;
for ( i = 0 ; i < n ; i++ )
cin >> s[i];
for ( i = 0 ; i < n - 1 ; i++ )
for ( j = n - 1 ; j > i ; j-- )
if ( s[j] < s[j - 1] )
swap( s[j - 1] , s[j] );
cout << s[0];
for ( i = 1 ; i < n ; i++ )
cout << " " << s[i];
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
三、箱排序
求的是n个1000以内的整数排序。
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int s[1001] = {}, n;
int i, j, t;
cin >> n;
for ( i = 0 ; i < n ; i++ )
{
cin >> t;
s[t]++;
}
for ( i = 0 , t = 0; i < 1001 ; i++ )
{
while (s[i] != 0)
{
t++;
if (t != 1) cout << " ";
cout << i;
s[i]--;
}
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}