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
| Property | Insertion sort | Counting sort |
|---|---|---|
| Technique | Insert each value into a sorted prefix | Count each value, turn the counts into positions and place the values, with no comparisons |
| Best case time | O(n) | O(n + k) |
| Average case time | O(n²) | O(n + k) |
| Worst case time | O(n²) | O(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 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.
| Input | Insertion sort | Counting sort |
|---|---|---|
| Mixed | 30 | 47 |
| Already sorted | 12 | 47 |
| Reversed | 48 | 47 |
| Nearly sorted | 14 | 47 |
| Duplicates | 36 | 47 |
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
- 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 Radix 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