Sub topic 12
Heap Sort vs. Quicksort and Merge Sort
This chapter assumes you know Big-O notation, the heap sort algorithm from earlier chapters, its O(n log n) time and O(1) extra space, and why it is not stable. You should also know the basic idea of quicksort, which partitions around a pivot, and merge sort, which splits the array, sorts the halves, and merges them.
All three algorithms reach O(n log n) in typical use. Which one to pick depends on worst-case guarantees, memory, stability, and how each one uses the hardware.
The Head-to-Head Numbers
| Property | Heap sort | Quicksort | Merge sort |
|---|---|---|---|
| Best case | O(n log n) | O(n log n) | O(n log n) |
| Average case | O(n log n) | O(n log n) | O(n log n) |
| Worst case | O(n log n) | O(n²) | O(n log n) |
| Extra space | O(1) | O(log n) stack, if it recurses on the smaller side first | O(n) for arrays |
| Stable | No | No (typical in-place versions) | Yes |
| Cache behavior | Poor | Excellent | Good |
| Typical speed on random arrays | Slowest of the three | Usually fastest | Close to quicksort |
Heap sort is the only one of the three that has both a guaranteed O(n log n) worst case and constant extra space. Both properties hold for any input.
Where Heap Sort Loses
Memory access patterns
Sift-down jumps from index i to 2i + 1. Near the top of a large heap those jumps are small. Lower down, parent and child sit far apart in memory, so a single sift-down can touch a new cache line at each level. Quicksort's partition scans the array sequentially from both ends, and the CPU prefetcher handles that pattern well. Merge sort also reads and writes in sequential runs.
Comparison count
Standard heap sort makes about 2n log₂ n comparisons in the worst case, because each sift-down level compares the two children and then compares the larger child with the parent. Randomized quicksort averages about 1.39n log₂ n comparisons, and merge sort uses fewer than n log₂ n. Heap sort is slower in practice because the constant factor is larger and the cache misses add up.
Stability
Swapping the root to the end moves elements across long distances, so equal keys can come out in a different order. If you sort records by a secondary key after sorting by a primary key, you need a stable sort such as merge sort.
Where Heap Sort Wins
- Hard real-time or adversarial input. Quicksort can be pushed to O(n²) by bad pivots or crafted input. Heap sort cannot be.
- Tight memory. Embedded systems and kernels often cannot allocate O(n) buffers. The Linux kernel's generic
sort()is a heap sort for this reason. - Partial sorting. If you need only the top k elements, build a heap in O(n) and pop k times, for O(n + k log n) total.
Choosing an Algorithm
Common Misconceptions
- "Same Big-O means same speed." The constant factors and cache behavior differ, so quicksort often beats heap sort by a factor of two or more on large random arrays.
- "Merge sort is always O(n) space." That holds for arrays. On linked lists, merge sort needs only O(1) extra space apart from recursion, because merging just relinks nodes.
- "Quicksort's worst case is rare, so ignore it." It is rare on random data. It is not rare when an attacker controls the input or the data is already sorted and the pivot choice is naive.
Exercise
A firmware routine must sort 10,000 sensor readings using no heap allocation, and it must meet a hard deadline regardless of input order. Stability does not matter. Which algorithm fits best, and why are the other two worse choices?
Show answer
Heap sort. It guarantees O(n log n) time on every input and uses O(1) extra space. Quicksort can degrade to O(n²) and miss the deadline. Merge sort on an array needs an O(n) buffer, which the routine cannot allocate.
Exercise
Heap sort and merge sort both run in O(n log n) worst-case time. Give two reasons merge sort is usually faster on large arrays.
Show answer
Merge sort makes fewer comparisons (under n log₂ n, against about 2n log₂ n for heap sort), and it reads and writes memory sequentially. Heap sort's parent-to-child jumps cause many cache misses.