Intuition
Selection sort scans the whole unsorted part to find the next value, which costs O(n) per pass. Heap sort keeps the unsorted part as a heap, where the largest value is always at the root. Taking it out and repairing the heap costs only O(log n), so the n passes cost O(n log n) in total.
How it works
It runs in two phases on the same array.
1. Build a max-heap. Treat the array as a binary tree: index 0 is the root, and the children of index i are 2i + 1 and 2i + 2. Starting from the last parent and working back to the root, sift down each value: compare its two children, and if the larger child is bigger than the parent, swap them and keep going down.
2. Sort. Repeat until the heap is one value:
- Swap the root (the largest value) with the last value in the heap. The largest value is now final at the end.
- Shrink the heap by one, so the final value is left alone.
- Sift the new root down to restore the heap.
In the figure, the first phase shuffles values toward the front without finishing any of them. Then the black, final section grows from the right, one root swap at a time.
Complexity
| Phase | Cost | Why |
|---|---|---|
| Build the heap | O(n) | Most values are near the bottom and sift only a step or two. |
| n − 1 root swaps | O(n log n) | Each sift-down walks at most one root-to-leaf path, log₂ n levels. |
| Total | O(n log n) | Best, average and worst alike. |
It sorts in place with O(1) extra memory, which merge sort can’t do. In practice it’s usually slower than quicksort, because sift-down jumps between distant indexes and makes poor use of the CPU cache.
Common mistakes
- Sifting up instead of down. Building a heap by inserting values one at a time and sifting each up works, but costs O(n log n) instead of O(n).
- Comparing with only one child. Sift-down must pick the larger child; swapping with the smaller one breaks the heap.
- Forgetting to shrink the heap. After a root swap, sift down only within the first
endvalues, or the sorted tail gets pulled back in.
When to use it
When you need a guaranteed O(n log n) sort with no extra memory, such as in embedded systems or kernels. It’s also the fallback inside introsort, the algorithm behind C++’s std::sort: quicksort runs until the recursion gets suspiciously deep, then heap sort takes over so the worst case stays O(n log n).
Check yourself
Check yourself
In a max-heap stored as an array, where are the children of index 3?
The children of index i are at 2i + 1 and 2i + 2, so 7 and 8.
Check yourself
After the heap is built, which value does the first root swap send to the end?
In a max-heap the largest value is at the root, so the first swap puts the maximum in its final place, the last index.
Sources
- J. W. J. Williams, “Algorithm 232: Heapsort,” Communications of the ACM 7, no. 6 (1964): 347–348.
- Robert W. Floyd, “Algorithm 245: Treesort 3,” Communications of the ACM 7, no. 12 (1964): 701. The O(n) heap construction.
- David Musser, “Introspective Sorting and Selection Algorithms,” Software: Practice and Experience 27, no. 8 (1997): 983–993.
Finished this page?
Your list