1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
void swap (int array[], int first_index, int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}
void displayArray (int array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
if ( i != 0 )
{
cout << ", ";
}
cout << array[ i ];
}
cout << "}";
}
int main ()
{
int array[ 10 ];
srand( time( NULL ) );
for ( int i = 0; i < 10; i++ )
{
// keep the numbers small so they're easy to read
array[ i ] = rand() % 100;
}
cout << "Original array: ";
displayArray( array, 10 );
cout << '\n';
sort( array, 10 );
cout << "Sorted array: ";
displayArray( array, 10 );
cout << '\n';
cin.get();
return 0;
}
