Just to add to lewax's suggested changes.
The first swap (of the smallest and i
th array elements) should be moved to after the first for loop (where the index of the smallest array element is found). Otherwise, selectionSort fails when the i
th element is the largest, and is swapped for the smallest before it can be swapped for the largest.
void selectionSort( int * const array, const int size )
{
int smallest;
int max;
int i=0;
while( i < size - i )
{
smallest = i;
max = i;
for ( int index = i; index < size-i; index++ )
{
if ( array[ index ] < array[ smallest ] )
smallest = index;
}
swap( &array[ i ], &array[ smallest ] );
for ( int x = i; x < size-i; x++ )
{
if ( array[ x ] > array[ max ] )
max = x;
}
swap( &array[ size - ( i + 1 ) ], &array[ max ] );
i++;
}
}