#include <iostream>
#include <iomanip>
using namespace std;
template< typename T >
void selectionSort( int [], const int, int (*)( int, int ) );
void swap( int * const, int * const );
int ascending( int, int );
void selectionSort( int work[], const int size, int (*compare)( int, int ) )
{
int smallestToLargest;
for ( int i = 0; i < size - i; ++i )
{
smallestToLargest = i;
for ( int index = i + 1; index < size; ++index )
if ( !(*compare)( work[ smallestToLargest ], work[ index ] ) )
smallestToLargest = index;
swap( &work[ smallestToLargest ], &work[ i ] );
}
}
void swap( int * const element1Ptr, int * const element2Ptr )
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
int ascending( int a, int b )
{
return a < b;
}
int main()
{
const int arraySize = 10;
int order;
int counter;
int a[ arraySize ] = { 2, 6, 4, 58, 10, 12, 89, 68, 45, 37 };
cout << "\nData items in original order\n";
for ( counter = 0; counter < arraySize; ++counter )
cout<< setw( 4 ) << a[ counter ];
cout << "\nData items in ascending order\n";
selectionSort( a, arraySize, ascending );
for ( counter = 0; counter < arraySize; ++counter )
cout << setw( 4 ) << a[ counter ];
cout << endl;
}
|