// Selection Sort Algorithm // C++ Example // www.TullyRankin.com #include using std::cout; using std::endl; #include using std::setw; void selectionSort( int * const, const int ); // prototype void swap( int * const, int * const ); // prototype int main() { const int arraySize = 10; int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Data items in original order \n"; for ( int i = 0; i < arraySize; i++ ) cout << setw( 4 ) << a[ i ]; selectionSort( a, arraySize ); // sort the array cout << "\nData items in ascending order\n"; for ( int j = 0; j < arraySize; j++ ) cout << setw( 4 ) << a[ j ]; cout << endl; return 0; } // end main // function to sort an array void selectionSort( int * const array, const int size ) { int smallest; // intdex of smallest element for ( int i = 0; i < size - 1; i++ ) { smallest = i; // loop to find idnex of smallest element for ( int index = i + 1; index < size; index++ ) if ( array[ index ] < array[ smallest ] ) smallest = index; swap( &array[ i ], &array[ smallest ] ); } } // swap values at memory locations to which // element1Ptr and element2Ptr point void swap( int * const element1Ptr, int * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; }