Selection Sort : Sort « Development « C++ Tutorial






#include <iostream> 
using std::cout; 
using std::endl; 

#include <iomanip>
using std::setw;

void selectionSort( int * const, const int );
void swap( int * const, int * const );

int main()
{
   const int arraySize = 10;
   int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 9, 1, 5, 7 };

   selectionSort( a, arraySize );
   for ( int j = 0; j < arraySize; j++ )
      cout << a[ j ];

   return 0;
}

void selectionSort( int * const array, const int size )
{
   int smallest;

   for ( int i = 0; i < size - 1; i++ )
   {
      smallest = i;

      for ( int index = i + 1; index < size; index++ )
         if ( array[ index ] < array[ smallest ] )
            smallest = index;
      swap( &array[ i ], &array[ smallest ] );
   }
}
void swap( int * const element1Ptr, int * const element2Ptr )
{
   int hold = *element1Ptr;
   *element1Ptr = *element2Ptr;
   *element2Ptr = hold;
}
124567891012








5.22.Sort
5.22.1.A Bubble sort
5.22.2.A recursive version of Quicksort for sorting characters
5.22.3.Quick Sort
5.22.4.how to declare your own function and function pointer to be used with qsort( )
5.22.5.Sort Tracer
5.22.6.Selection Sort