Skip to content
SimpleScope

Sorting

Heap sort vs Radix sort

Heap sort and Radix sort both put an array in order, in different ways. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.

At a glance

PropertyHeap sortRadix sort
TechniqueBuild a max-heap in the array, then repeatedly move the largest value to the endSort digit by digit, least significant first, with a stable counting sort for each digit
Best case timeO(n log n)O(d (n + k))
Average case timeO(n log n)O(d (n + k))
Worst case timeO(n log n)O(d (n + k))
Extra spaceO(1)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)NoYes
In place (no extra array)YesNo

When to choose each

Heap sort: when to choose it

Choose heap sort when the keys are not fixed-width integers or when memory is tight. It is the only sort here that is O(n log n) in the worst case and O(1) in extra space at the same time, and it makes no assumption about the values beyond comparability. Radix sort needs integral keys of bounded width and allocates count and output arrays for every digit pass, so its memory is O(n + k) and its pass count is set by the key format.

Radix sort: when to choose it

Choose radix sort for large volumes of fixed-width integer keys, where it is asymptotically faster than any comparison sort including heap sort: O(d(n + k)) with no comparisons at all, and with d fixed that is linear in n. It is stable, which heap sort is not, and its cache behaviour is sequential where heap sort jumps between parents and children. The trade is memory and the restriction on what a key may be.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes). Radix sort makes no comparisons, so its count is writes only. The lesson always runs 3 digit passes, even on these one-digit numbers.

InputHeap sortRadix sort
Mixed43141
Already sorted53141
Reversed40141
Nearly sorted50141
Duplicates36141

What the numbers show

Heap sort wins all five arrays at this size, by factors of 2.7 to 3.9: 43 operations against 141 on the mixed array, 53 against 141 on the sorted one, 40 against 141 on the reversed one, 50 against 141 on the nearly sorted one and 36 against 141 on the duplicates. Neither algorithm varies much with input order, so the five rows tell one story.

Radix sort's 141 is 3 × 47: three digit passes, each a complete stable counting sort over seven values and a range of ten. Two of those passes sort these one-digit numbers by a tens and a hundreds digit that are all zero. Heap sort's totals come from 16 to 21 comparisons plus 20 to 32 writes, the writes being root-to-end swaps and the sift-downs that repair the heap afterwards.

The asymptotics run the other way, which is the whole reason to keep radix sort in the curriculum. Heap sort's work grows as roughly 2n log₂ n, so every doubling of the array costs more than double; radix sort's grows as d(n + k) with d fixed, so doubling the array doubles the work. At a million three-digit keys that is tens of millions of operations against roughly three million. Read these counts as the cost of three passes amortised over only seven elements.

See them run

More comparisons