public static void topK ( Quicksortable qs , int totalSize , int k ) { if ( k > totalSize ) k = totalSize ; makeHeap ( qs , k ) ; for ( int i = k ; i < totalSize ; i ++ ) { // compare each element to the root of the heap if ( qs . compare ( i , 0 ) > 0 ) { // if it's greater, swap it out and push it down qs . swap ( 0 , i ) ; heapifyDown ( qs , 0 , k ) ; } } sort ( qs , k ) ; reverse ( qs , k ) ; }
in an unspecified order