Build a max-heap, then repeatedly swap its root with the last element of the active prefix, shrink that prefix and restore heap order by heapify down. A zero-based implementation is
for i = floor(n/2)-1,...,0:
    siftDown(A,i,n)
for end = n-1,...,1:
    swap A[0],A[end]
    siftDown(A,0,end)
siftDown(A,i,end) repeatedly exchanges entry i with its larger child while the child lies below end and has a larger key. The suffix beyond end is already sorted and never touched again. For the specified input, key stages of heapsort are
StageActive max heapSorted suffix
Bottom-up heap construction
Extract 5, repair
Extract 4, repair
Extract 3, repair
Extract 2
The final array is . Bottom-up construction takes time because there are progressively fewer nodes at each greater subtree height; the extractions give worst-case time and constant auxiliary storage for iterative sifting.

Articles by others on the same topic (0)

There are currently no matching articles.