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
| Property | Insertion sort | Radix sort |
|---|---|---|
| Technique | Insert each value into a sorted prefix | Sort digit by digit, least significant first, with a stable counting sort for each digit |
| Best case time | O(n) | O(d (n + k)) |
| Average case time | O(n²) | O(d (n + k)) |
| Worst case time | O(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) | Yes | Yes |
| In place (no extra array) | Yes | No |
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.
| Input | Insertion sort | Radix sort |
|---|---|---|
| Mixed | 30 | 141 |
| Already sorted | 12 | 141 |
| Reversed | 48 | 141 |
| Nearly sorted | 14 | 141 |
| Duplicates | 36 | 141 |
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
- 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
- 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
- Heap sort vs Radix sort
- Counting sort vs Radix sort