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
| Property | Heap sort | Radix sort |
|---|---|---|
| Technique | Build a max-heap in the array, then repeatedly move the largest value to the end | Sort digit by digit, least significant first, with a stable counting sort for each digit |
| Best case time | O(n log n) | O(d (n + k)) |
| Average case time | O(n log n) | O(d (n + k)) |
| Worst case time | O(n log n) | O(d (n + k)) |
| Extra space | O(1) | O(n + k) |
| Compares values with each other | Yes | No |
| Stable (equal values keep their order) | No | Yes |
| In place (no extra array) | Yes | No |
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.
| Input | Heap sort | Radix sort |
|---|---|---|
| Mixed | 43 | 141 |
| Already sorted | 53 | 141 |
| Reversed | 40 | 141 |
| Nearly sorted | 50 | 141 |
| Duplicates | 36 | 141 |
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
- 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 Heap 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
- Counting sort vs Radix sort