public static void partialHeapSort ( Quicksortable q , int k , int size ) { makeHeap ( q , size ) ; for ( int i = 0 ; i < k ; i ++ ) { q . swap ( 0 , size - i - 1 ) ; heapifyDown ( q , 0 , size - i - 1 ) ; } vecswap ( q , 0 , size - k , k ) ; reverse ( q , k ) ; }
finds the lowest k elements of q and stores them in sorted order at the beginning of q by turning q into a heap .