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
| Property | Bubble sort | Counting sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | 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
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.
| Input | Bubble sort | Counting sort |
|---|---|---|
| Mixed | 40 | 47 |
| Already sorted | 6 | 47 |
| Reversed | 63 | 47 |
| Nearly sorted | 13 | 47 |
| Duplicates | 47 | 47 |
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
- 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 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
- 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