Skip to content
SimpleScope

Sorting

Bubble sort vs Counting sort

Bubble sort and Counting sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons.

At a glance

PropertyBubble sortCounting sort
TechniqueSwap neighbors that are out of orderCount 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

Bubble sort: when to choose it

Choose bubble sort when you cannot say anything about the values. It sorts anything that can be compared — strings, records, floats, objects with a custom order — and it needs no extra memory. Counting sort needs the values to be whole numbers in a known, small range, because it uses each value as an index into a count array. Bubble sort is also the cheaper of the two on a small array that is already close to order, since it can stop early and counting sort cannot stop at all.

Counting sort: when to choose it

Choose counting sort when the keys are small whole numbers and n is large. It never compares two values, so the O(n log n) lower bound on comparison sorting simply does not apply to it: the cost is O(n + k) for a range of k, and it is the same cost whatever order the input arrived in. It is stable, which is what makes it usable as the inner pass of radix sort, and it is the right answer for things like ages, grades, bucketed counters or byte values.

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.

InputBubble sortCounting sort
Mixed4047
Already sorted647
Reversed6347
Nearly sorted1347
Duplicates4747

What the numbers show

Counting sort costs exactly 47 operations on every one of the five arrays — mixed, sorted, reversed, nearly sorted, duplicates — and every one of them is a write. Zero comparisons, which is why the table carries a "compares values with each other" row saying No. The run fills a count array of k + 1 = 10 cells, adds one per input value, turns the counts into positions, then places and copies back each of the seven values; none of that depends on the arrangement of the input.

Bubble sort's totals, by contrast, span 6 to 63. It wins the three cases where the input has order to exploit — 6 against 47 on the sorted array, 13 against 47 on the nearly sorted one, and 40 against 47 on the mixed array. The duplicates array is a dead heat, 47 against 47. Counting sort wins only the reversed array, 47 against 63.

Seven elements over a range of ten is the worst possible shape for counting sort: k is larger than n, so the count array itself dominates the work. Its advantage is in the other direction. Sort ten thousand values in the same 0–9 range and counting sort still costs about n + k operations while bubble sort costs about n²/2 — tens of thousands against tens of millions. The question to answer before choosing it is never "how many values", it is "how wide is the range".

See them run

More comparisons