Skip to content
SimpleScope

Sorting

Insertion sort vs Counting sort

Insertion sort and Counting sort both put an array in order, in different ways. Insertion sort: insert each value into a sorted prefix. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons.

At a glance

PropertyInsertion sortCounting sort
TechniqueInsert each value into a sorted prefixCount each value, turn the counts into positions and place the values, with no comparisons
Best case timeO(n)O(n + k)
Average case timeO(n²)O(n + k)
Worst case timeO(n²)O(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 when you cannot constrain the values or cannot allocate. It compares, so it sorts anything with an order; counting sort indexes an array by value, so it needs whole numbers in a known, narrow range. Insertion sort is also adaptive: on data that is already nearly in order its comparison count drops towards n, and counting sort has no such mode — its cost is identical on sorted and reversed input, which is both its strength and, at this size, its handicap.

Counting sort: when to choose it

Choose counting sort when the keys are small whole numbers and the array is large. It makes no comparisons at all, so the O(n log n) comparison bound does not constrain it and insertion sort's O(n²) average is not even in the same class: counting sort is O(n + k) on every input. Both are stable, so records with equal keys keep their order either way; the difference is that counting sort's guarantee does not depend on the data and insertion sort's good cases do.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes). Counting sort makes no comparisons, so its count is writes only. It includes the k + 1 cells of the count array.

InputInsertion sortCounting sort
Mixed3047
Already sorted1247
Reversed4847
Nearly sorted1447
Duplicates3647

What the numbers show

Counting sort costs 47 operations on all five arrays, every one a write. Insertion sort wins four of them: 12 against 47 on the already-sorted array, 14 against 47 on the nearly sorted one, 30 against 47 on the mixed array and 36 against 47 on the duplicates. Counting sort wins only the reversed array, 47 against 48 — and by one operation.

Those 47 operations are mostly fixed overhead at this size: a count array of k + 1 = 10 cells zeroed, one increment per value, 9 prefix sums, then a decrement, an output write and a copy back for each of the seven values. With k = 9 and n = 7 the count array is larger than the data. Insertion sort on the sorted array, by contrast, does 6 comparisons and 6 writes and stops.

The reversed array is the preview of what happens at scale: insertion sort hits its worst case at 48 while counting sort does not notice the input order at all. Push n up with k unchanged and that becomes decisive — ten thousand values in the 0–9 range cost counting sort on the order of n + k and insertion sort about n²/4, tens of thousands against tens of millions. The question is always the range, not the length.

See them run

More comparisons