Sorting
Merge sort vs Heap sort
Merge sort and Heap sort both put an array in order, in different ways. Merge sort: divide and conquer: split, sort each half, merge. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end.
At a glance
| Property | Merge sort | Heap sort |
|---|---|---|
| Technique | Divide and conquer: split, sort each half, merge | Build a max-heap in the array, then repeatedly move the largest value to the end |
| Best case time | O(n log n) | O(n log n) |
| Average case time | O(n log n) | O(n log n) |
| Worst case time | O(n log n) | O(n log n) |
| Extra space | O(n) | O(1) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | No |
| In place (no extra array) | No | Yes |
When to choose each
Merge sort: when to choose it
Choose merge sort when you need stability, or when the data is on disk or in a linked structure. Both sorts guarantee O(n log n), so the choice between them is not about time: it is that merge sort keeps equal elements in their original order and reads its inputs sequentially, which matters for records sorted by several keys in turn and for anything streamed. The price is O(n) extra memory.
Heap sort: when to choose it
Choose heap sort when memory is the binding constraint. It gives the same O(n log n) worst-case guarantee in O(1) extra space, because the heap is built inside the array being sorted — the only sort on this page with both properties. You give up stability, and you give up locality: sifting a value down jumps between indexes i, 2i + 1 and 2i + 2, which is harder on a cache than merge sort's sequential scans.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).
| Input | Merge sort | Heap sort |
|---|---|---|
| Mixed | 34 | 43 |
| Already sorted | 31 | 53 |
| Reversed | 29 | 40 |
| Nearly sorted | 31 | 50 |
| Duplicates | 32 | 36 |
What the numbers show
Merge sort is cheaper on all five inputs at this size: 34 operations against 43 on the mixed array, 31 against 53 on the already-sorted one, 29 against 40 on the reversed one, 31 against 50 on the nearly sorted one and 32 against 36 on the duplicates. Both are O(n log n), so this is a constant-factor result, not an asymptotic one.
The constant is where the two differ structurally. Merge sort writes each element back once per level of recursion, a fixed 20 writes here, and compares 9 to 14 times. Heap sort writes 20 to 32 times, because each extraction swaps the root to the end and then sifts a value down through several levels, and compares 16 to 21 times. Moving a value through a heap costs more writes than copying it out of a sorted half.
Heap sort's worst input of the five is the one that was already sorted, 53, which is worth sitting with: building a max-heap from ascending values has the most work to do, and heap sort has no adaptive mode to recover it. Merge sort's cost is just as indifferent to order — 29 to 34 — but its constant is smaller. Pick between them on memory and stability, not on these totals.
See them run
More comparisons
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort