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. Radix sort compares no values: it takes O(d (n + k)), where k is the number of possible values (or digit values) and d the number of digits (radix sort), while merge sort takes O(n log n) in the worst case. Radix sort is faster only when k and d are small compared with n; merge sort makes no assumption about the values.

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
Compares values with each otherYesNo
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)
Stable (equal values keep their order)YesYes
In place (no extra array)NoNo
Needs sorted inputNoNo

When to choose each

Choose merge sort when

you need a guaranteed O(n log n) time or a stable sort.

Avoid it when

memory is tight.

Choose radix sort when

many integers or fixed-length keys have few digit positions (d is small).

Avoid it when

the array is small, or the keys are long.

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

See them run

More comparisons