AlgoLab

O(d(n + b))·O(n + b) space·stable
60599267494414214037

Starting radix sort on an array of 20 items.

Starting radix sort on an array of 20 items.
  1. 1starting from the last digit, repeat the following for each digit place
  2. 2count how many values have each digit in the current place
  3. 3turn those counts into positions by adding each one to the count of all smaller digits
  4. 4working from the last value to the first, place each value at its position for this digit and reduce that count by one
  5. 5once every digit place has been processed, the array is fully sorted
Count updates (per digit)
0
Writes
0
Accesses
0
Comparisons (stays 0 — radix sort never compares values)
0

Complexity

BestO(d(n + b))
AverageO(d(n + b))
WorstO(d(n + b))
Extra spaceO(n + b)
StableYes
n at n=20 predicts 20; this run took 0.

Radix sort (LSD)

Radix sort (least-significant-digit first) sorts numbers by repeatedly running a stable counting sort on one digit at a time, starting from the ones place and working up through the tens, hundreds, and so on. After every digit place has been processed, the array is fully sorted.

When it is the right choice

Radix sort is worth using for sorting large collections of fixed-width integers, where its O(d(n + b)) time — d digit passes over n values, each pass a base-b counting sort — beats a comparison sort's O(n log n) once n is large enough relative to the digit count d. It is a poor fit for values with many digits or an unbounded range, since every extra digit is another full pass over the whole array.

How it works

Worth noticing

Cost

Best
O(d(n + b))
Average
O(d(n + b))
Worst
O(d(n + b))
Extra space
O(n + b)
Stable
Yes

Worth comparing

All algorithms