Sorting
Merge sort vs Radix sort
Merge sort and Radix sort both put an array in order, in different ways. Merge sort: divide and conquer: split, sort each half, merge. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.
At a glance
| Property | Merge sort | Radix sort |
|---|---|---|
| Technique | Divide and conquer: split, sort each half, merge | 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(n) | O(n + k) |
| Compares values with each other | Yes | No |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | No | No |
When to choose each
Merge sort: when to choose it
Choose merge sort when the keys do not decompose into a small fixed number of digits, or when you want one algorithm that handles every comparable type. It makes no assumption about the values and its memory is O(n). Radix sort needs fixed-width integral keys and runs a complete stable counting sort per digit position, so a key with many digits multiplies the number of passes directly. Both sorts are stable.
Radix sort: when to choose it
Choose radix sort for large volumes of fixed-width keys, where it beats every comparison sort including merge sort. Its cost is O(d(n + k)) with no comparisons at all, so it is not subject to the O(n log n) lower bound: with d fixed, the cost is linear in n while merge sort's grows as n log n. It is stable by construction, since each digit pass must preserve the previous pass's order. The cost is memory per pass and the key-format requirement.
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 | Merge sort | Radix sort |
|---|---|---|
| Mixed | 34 | 141 |
| Already sorted | 31 | 141 |
| Reversed | 29 | 141 |
| Nearly sorted | 31 | 141 |
| Duplicates | 32 | 141 |
What the numbers show
At this size merge sort wins all five cases by a factor of more than four: 34 operations against 141 on the mixed array, 31 against 141 on the sorted one, 29 against 141 on the reversed one, 31 against 141 on the nearly sorted one and 32 against 141 on the duplicates. Both are essentially indifferent to input order, so there is no case-by-case story to tell — only a constant-factor one.
The constant is explicit here: 141 is 3 × 47, three digit passes each costing a full counting sort over seven values and a range of ten. These arrays contain one-digit numbers, so two of those three passes sort by digits that are all zero. Merge sort needs 20 writes and 9 to 14 comparisons for the whole job.
What the counts cannot show is the asymptotic direction. Merge sort's work grows as n log₂ n, so every doubling of the array costs slightly more than double; radix sort's grows as d(n + k) with d fixed by the key width, so doubling the array exactly doubles the work. At a million three-digit keys that is on the order of 20 million operations for merge sort against about 3 million for radix sort. The things to check first are d and whether the keys are integral at all.
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
- 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