Skip to content
SimpleScope

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

PropertyMerge sortHeap sort
TechniqueDivide and conquer: split, sort each half, mergeBuild a max-heap in the array, then repeatedly move the largest value to the end
Best case timeO(n log n)O(n log n)
Average case timeO(n log n)O(n log n)
Worst case timeO(n log n)O(n log n)
Extra spaceO(n)O(1)
Compares values with each otherYesYes
Stable (equal values keep their order)YesNo
In place (no extra array)NoYes

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).

InputMerge sortHeap sort
Mixed3443
Already sorted3153
Reversed2940
Nearly sorted3150
Duplicates3236

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