Skip to content
SimpleScope

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

PropertyMerge sortRadix sort
TechniqueDivide and conquer: split, sort each half, mergeSort 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(n)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)YesYes
In place (no extra array)NoNo

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.

InputMerge sortRadix sort
Mixed34141
Already sorted31141
Reversed29141
Nearly sorted31141
Duplicates32141

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