Skip to content
SimpleScope

Sorting

Insertion sort vs Radix sort

Insertion sort and Radix sort both put an array in order, in different ways. Insertion sort: insert each value into a sorted prefix. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.

At a glance

PropertyInsertion sortRadix sort
TechniqueInsert each value into a sorted prefixSort digit by digit, least significant first, with a stable counting sort for each digit
Best case timeO(n)O(d (n + k))
Average case timeO(n²)O(d (n + k))
Worst case timeO(n²)O(d (n + k))
Extra spaceO(1)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)YesYes
In place (no extra array)YesNo

When to choose each

Insertion sort: when to choose it

Choose insertion sort for small arrays, nearly ordered data, and keys that are not fixed-width integers. It needs no extra memory and no assumption about the values beyond a comparison. Radix sort has to decompose each key into a bounded number of digits and run a full stable counting sort for each digit position, so for seven one-digit numbers it is doing three passes where insertion sort does one partial scan per element.

Radix sort: when to choose it

Choose radix sort for large volumes of fixed-width keys — integers, zero-padded identifiers, dates as numbers. Its cost is O(d(n + k)) with no comparisons, so it is not bound by O(n log n) and, unlike insertion sort, its per-element cost does not grow with the array; only the key width does. Both are stable. The practical constraints are the extra memory per pass and the requirement that the key really does split into a small, fixed number of digits.

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.

InputInsertion sortRadix sort
Mixed30141
Already sorted12141
Reversed48141
Nearly sorted14141
Duplicates36141

What the numbers show

Radix sort costs 141 operations on every one of the five arrays, and insertion sort wins all five: 12 against 141 on the already-sorted array, 14 against 141 on the nearly sorted one, 30 against 141 on the mixed array, 36 against 141 on the duplicates, and 48 against 141 even in insertion sort's own worst case.

That 141 is exactly 3 × 47: the lesson always runs 3 digit passes, and each is a complete counting sort costing 47 operations on a seven-element array over a range of ten. These arrays hold one-digit values, so two of the three passes sort by digits that are all zero — real work that provably cannot change the array. Insertion sort's best case here is 12 operations and its worst 48.

The crossover is driven by n, since d is fixed by the key format. Insertion sort's average is about n²/4, which quadruples when the array doubles; radix sort's is d(n + k), which merely doubles. For a thousand three-digit keys that is roughly 250,000 operations against roughly 3,000. Read the table as a demonstration that d passes need a large n to amortise, not as a verdict on radix sort.

See them run

More comparisons