Skip to content
SimpleScope

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

PropertyBubble sortRadix sort
TechniqueSwap neighbors that are out of orderSort digit by digit, least significant first, with a stable counting sort for each digit
Best case timeO(n)O(d (n + k))
Average case timeO(n²)O(d (n + k))
Worst case timeO(n²)O(d (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 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.

InputBubble sortRadix sort
Mixed40141
Already sorted6141
Reversed63141
Nearly sorted13141
Duplicates47141

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