Skip to content
SimpleScope

Sorting

Quick sort vs Radix sort

Quick sort and Radix sort both put an array in order, in different ways. Quick sort: divide and conquer: partition around a pivot. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.

At a glance

PropertyQuick sortRadix sort
TechniqueDivide and conquer: partition around a pivotSort digit by digit, least significant first, with a stable counting sort for each digit
Best case timeO(n log n)O(d (n + k))
Average case timeO(n log n)O(d (n + k))
Worst case timeO(n²)O(d (n + k))
Extra spaceO(log n)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)NoYes
In place (no extra array)YesNo

When to choose each

Quick sort: when to choose it

Choose quick sort for arrays of moderate size, for keys that are not fixed-width integers, and when memory is constrained. It sorts in place with O(log n) stack and makes no assumption about the values beyond a comparison. Radix sort requires integral keys of bounded width and runs a full stable counting sort per digit position, allocating arrays each time — on short arrays that is all setup and no payoff.

Radix sort: when to choose it

Choose radix sort for large volumes of fixed-width integer keys. Zero comparisons means the O(n log n) bound does not apply, and with d fixed its cost is linear in n where quick sort's grows as n log n. It is stable, and it has no worst case driven by input order — quick sort under a last-element pivot does, and sorted input triggers it. The costs are memory per pass and the requirement that keys split into a small number of digits.

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.

InputQuick sortRadix sort
Mixed31141
Already sorted75141
Reversed51141
Nearly sorted65141
Duplicates29141

What the numbers show

Radix sort costs 141 operations on all five arrays, and quick sort wins all five — but look at the margins. On the mixed array it is 31 against 141 and on the duplicates 29 against 141, comfortable factors of four or five. On the reversed array it is 51 against 141; on the already-sorted array 75 against 141, and on the nearly sorted one 65 against 141: quick sort's worst case is now within a factor of two of radix sort's flat cost.

Both numbers decompose cleanly. Radix sort's 141 is 3 × 47 — three digit passes, each a complete counting sort, two of them sorting by digits that are all zero on these one-digit values. Quick sort's 75 on sorted input is 21 comparisons and 54 writes, the Lomuto partition peeling off one element per level of recursion.

The shapes diverge from there. Quick sort's average work grows as n log n and its worst case as n²; radix sort's grows as d(n + k) with d set by the key width, so doubling the array doubles the work and nothing else. For a million three-digit keys that is on the order of 28 million comparisons-plus-writes for quick sort against about 3 million operations for radix sort — and radix sort's figure does not depend on the input arriving in a convenient order.

See them run

More comparisons