/**//** * Simple insertion sort. */ template <typename Comparable> void insertionSort( vector<Comparable>& a ) ...{ for( int p =1; p < a.size( ); p++ ) ...{ Comparable tmp = a[ p ]; int j; for( j = p; j >0&& tmp < a[ j -1 ]; j-- ) a[ j ] = a[ j -1 ]; a[ j ] = tmp; } } /**//** * Return median of left, center, and right. * Order these and hide the pivot. */ template <typename Comparable> const Comparable & median3( vector<Comparable>& a, int left, int right ) ...{ int center = ( left + right ) /2; if( a[ center ] < a[ left ] ) swap( a[ left ], a[ center ] ); if( a[ right ] < a[ left ] ) swap( a[ left ], a[ right ] ); if( a[ right ] < a[ center ] ) swap( a[ center ], a[ right ] ); // Place pivot at position right - 1 swap( a[ center ], a[ right -1 ] ); return a[ right -1 ]; } /**//** * Internal selection method that makes recursive calls. * Uses median-of-three partitioning and a cutoff of 10. * Places the kth smallest item in a[k-1]. * a is an array of Comparable items. * left is the left-most index of the subarray. * right is the right-most index of the subarray. * k is the desired rank (1 is minimum) in the entire array. */ template <typename Comparable> void quickSelect( vector<Comparable>& a, int left, int right, int k ) ...{ if( left +10<= right ) ...{ Comparable pivot = median3( a, left, right ); // Begin partitioning int i = left, j = right -1; for( ; ; ) ...{ while( a[ ++i ] < pivot ) ...{ } while( pivot < a[ --j ] ) ...{ } if( i < j ) swap( a[ i ], a[ j ] ); else break; } swap( a[ i ], a[ right -1 ] ); // Restore pivot // Recurse; only this part changes if( k <= i ) quickSelect( a, left, i -1, k ); elseif( k > i +1 ) quickSelect( a, i +1, right, k ); } else// Do an insertion sort on the subarray insertionSort( a, left, right ); }