日本語

NON-COMPARISON SORTS

Radix Sort, visualised

Radix sort reorders numbers digit by digit. This app processes base-10 digits starting from the least significant (ones) digit.

Run Radix Sort in the visualizer →

Opens the Sorting Cinema visualizer with 16 elements in English.

How it works

  1. Process the ones digit, then the tens digit, and so on, for as many digits as the maximum has.
  2. For each digit position, count how many elements have each digit 0–9.
  3. Use the counts to place each element, keeping elements with the same digit in their previous order.
  4. After the most significant digit, the array is sorted.

Complexity and stability

Best
O(d(n + b))
Average
O(d(n + b))
Worst
O(d(n + b))
Extra space
O(n + b)
Stability
Stable (each digit pass is stable)

With d digits and base b, the rule of thumb is O(d(n + b)) (as stated in the learning guide; this app uses b = 10). Each pass uses an output array and per-digit counts. With many digits it is not always faster than comparison sorts.

Source of facts: Complexity follows the learning guide's comparison table and text, and this app's implementation.

Trace on a small array

The first operations on the input [25, 3, 41, 12, 30], following the same steps as the app.

#OperationArray after
1Reorder by the ones digit[30, 41, 12, 3, 25]
2Reorder by the tens digit[3, 12, 25, 30, 41]

In this trace, 10 compare / swap / write operations in total produce [3, 12, 25, 30, 41].

Suitable inputs and caveats

  • Suited to positive integers with few digits; this app handles positive integers.
  • After the first pass only the ones digits line up; the array becomes sorted as higher digits are processed.
  • Each pass keeps equal digits in order, so it is stable.

FAQ

Why does radix sort start from the lowest digit?

Each digit pass is stable, so re-sorting by higher digits while preserving lower-digit order leaves the whole array sorted at the end (this app uses this LSD approach).

How does radix sort differ from counting sort?

Counting sort counts whole values; radix sort counts digits 0–9 per position. It copes with a wide value range as long as there are few digits.