选择排序(Selection Sort)
C/C++代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
void Print( int r[], int n )
{
for( int i = 0; i < n; i++ )
{
cout << r[i] << " ";
}
cout << endl;
}
void Swap( int& a, int & b )
{
int temp = a;
a = b;
b = temp;
}
void SelectionSort( int r[], int n )
{
for( int i = 0; i < n - 1; i++ )
{
int min = i;
for( int j = i + 1; j < n; j++ )
{
if( r[j] < r[min] ) min = j;
}
Swap( r[i], r[min] );
}
}
int main()
{
int r[] = { 34, 12, 45, 78, 52, 18, 67, 90, 64, 72, 27 };
int n = sizeof( r ) / sizeof( int );
Print( r, n );
SelectionSort( r, n );
Print( r, n );
system( "PAUSE" );
return 0;
}