Heap sort is an in-place sorting algorithm that uses a heap data structure.
It first transforms the list into a max-heap, then repeatedly pops the maximum value and places it at the end of the list.
To build the heap in-place, the list is treated as a complete binary tree.
With this assumption, it traverses from the bottom up and applies downHeap to convert the binary tree into a valid heap.
This step takes $O(n \log n)$ (in fact, it is known to run in $O(n)$ with a careful analysis).
After that, repeatedly swapping the root with the last element and restoring the heap property yields the sorted result.
Overall, heap sort is an $O(n \log n)$ in-place sorting algorithm.
1 | |