Sorting
Bubble sort vs Radix sort
Bubble sort and Radix sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.
At a glance
| Property | Bubble sort | Radix sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | 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
Bubble sort: when to choose it
Choose bubble sort on short arrays, on anything that is not a fixed-length integer key, and whenever extra memory is unavailable. Radix sort's cost is d passes over the data, where d is the number of digit positions, and each pass is a full stable counting sort with its own arrays. On seven one-digit numbers that structure is all overhead. Bubble sort compares and swaps in place and, on input that is already ordered, finishes in a single pass.
Radix sort: when to choose it
Choose radix sort when you have many fixed-width keys: integers, dates as YYYYMMDD, fixed-length identifiers. Its cost is O(d(n + k)) with no comparisons at all, so unlike bubble sort it is not bound by O(n log n) and its cost per element does not grow as the array grows — only the number of digit positions matters. It is stable by construction, which it has to be: each pass must preserve the order the previous, less significant pass established, or the result is wrong.
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 | Bubble sort | Radix sort |
|---|---|---|
| Mixed | 40 | 141 |
| Already sorted | 6 | 141 |
| Reversed | 63 | 141 |
| Nearly sorted | 13 | 141 |
| Duplicates | 47 | 141 |
What the numbers show
Radix sort costs 141 operations on every one of the five arrays, the largest number anywhere in this comparison, and all of them are writes. The reason is visible in the lesson itself: this implementation always runs 3 digit passes, so one-digit numbers get sorted by their tens and hundreds digits too — two passes that cannot change anything. Each pass is a complete counting sort at 47 operations, so the total is exactly three times that.
Bubble sort beats it on all five inputs, and not narrowly: 6 against 141 on the already-sorted array, 13 against 141 on the nearly sorted one, 40 against 141 on the mixed array, 47 against 141 on the duplicates, and even in bubble sort's own worst case, the reversed array, 63 against 141.
Do not read that as radix sort being slow; read it as the wrong tool shown at the wrong size. Its per-element cost is fixed at d(1 + k/n)-ish work rather than growing with n, so the crossover is a function of array length, not of the values. At n = 1,000 three-digit keys, radix sort does on the order of 3(n + 10) ≈ 3,000 operations where bubble sort does about n²/2 ≈ 500,000. The quantity to check before choosing it is d, and whether n is big enough to amortise d passes.
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
- 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