Skip to content

Radix sort

ordering

Level 3 of 3

Also called Least significant digit radix sort.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Radix sort starts on 503, 72, 318, 46, 211, 39, 125. What is the row after the first pass?

5037231846211391250123456789
the answer, and why

the answer is D

The first pass sorts by the ones digit, 1, 2, 3, 5, 6, 8 and 9.

if you chose another

  • That is after two passes, ones then tens.
  • That is the end, after all three passes.
  • That sorts by the hundreds digit first.

question 2

How many placings does radix sort make on 305, 7, 42, 618?

3057426180123456789
the answer, and why

the answer is C

The biggest, 618, has three digits, so 3 passes. Each places all 4 numbers.

if you chose another

  • That is one pass, or the numbers in the row.
  • That is the totals added.
  • That adds every count, addition and placing.

question 3

On which row does radix sort make the fewest placings?

the answer, and why

the answer is D

One digit each, so one pass of 6 placings.

if you chose another

  • 10 has two digits, so two passes, 12 placings.
  • 100 has three digits, so three passes, 18 placings.
  • In order, but seven numbers, 7 placings.

question 4

Which is true of radix sort on numbers from 0 to 999?

the answer, and why

the answer is C

How many steps it takes never depends on the order, only on how many numbers there are and the biggest one's digits, which set the passes.

this tests best, worst and average, on the learn page

if you chose another

  • The order never changes how many steps it takes.
  • With at most three digits, it makes at most 3n3n placings.
  • It compares no two numbers.

pen and paper

question 5about 10 minutes

Radix sort works on 520, 37, 804, 61.

  1. Write the row after each pass.
  2. Count the placings, and check against 3n3n.
  3. In the hundreds pass, 37 and 61 share the digit 0. Why does 37 stay first?
a hint

Read 37 as 037 and 61 as 061.

a worked answer

After the ones pass: 520, 61, 804, 37. After the tens pass: 804, 520, 37, 61. After the hundreds pass: 37, 61, 520, 804. Three passes of 4 placings make 12, and 3 times 4 is 12. In the hundreds pass the total for digit 0 is 2. From the back, 61 is sent first, to place 2, then 37 to place 1, so 37 stays before 61.

Why does radix sort make exactly 3n3n placings when the biggest has three digits?
  1. the move: price one pass

    the line:
    one pass sendseach number once\begin{gathered} \text{one pass sends} \\ \text{each number once} \end{gathered}
    sent from the last number back130527342461812346184421733052
    The ones pass on 305, 7, 42, 618. From the back, 618 goes to place 4, 42 to place 1, 7 to place 3 and 305 to place 2. Four placings.

    why it is allowed: From the last number back to the first, each number is sent to its place once. The picture is the ones pass on 305, 7, 42, 618.

  2. the move: count the passes

    the line:
    three digits,so three passes\begin{gathered} \text{three digits,} \\ \text{so three passes} \end{gathered}
    afterthe row
    the start305, 7, 42, 618
    the ones pass42, 305, 7, 618
    the tens pass305, 7, 618, 42
    the hundreds pass7, 42, 305, 618
    305, 7, 42, 618 after each of its three passes. Each pass sends all four numbers.

    why it is allowed: One pass for each digit of the biggest number, ones, tens and hundreds. The biggest, 618, has three digits.

  3. the move: add the passes

    the line:
    P=n+n+nP = n + n + n

    why it is allowed: PP is the placings made. By lines 1 and 2, nn placings in each of 3 passes.

  4. the move: gather

    the line:
    P=3nP = 3n

    why it is allowed: By line 3, three lots of nn. For 305, 7, 42, 618, that is 12.

  5. the move: a ceiling

    the line:
    P≤3nP \le 3n

    why it is allowed: By line 4, PP is 3n3n, so it is at most 3n3n.

  6. the move: read it as a bound

    the line:
    P∈O(n)P \in O(n)

    why it is allowed: By line 5, PP is at most 3 times nn.

  7. the move: a floor

    the line:
    P≥3nP \ge 3n

    why it is allowed: By line 4, PP is at least 3n3n too.

  8. the move: read it as a bound

    the line:
    P∈Ω(n)P \in \Omega(n)

    why it is allowed: By line 7, PP never drops below 3 times nn.

  9. the move: ceiling and floor together

    the line:
    P∈Θ(n)P \in \Theta(n)

    why it is allowed: Lines 6 and 8 bound PP by the same growth, nn.

try it with a small nn: at n=4n = 4, line 3 gives P=12P = 12, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • The three rows.
  • 12 placings, matching the formula.
  • 61 sent first to place 2, 37 to place 1.

question 6about 6 minutes

Say you sort 1,000 different whole numbers from 0 up.

  1. Why is the biggest at least 999?
  2. At least how many passes, and how many placings?
  3. And for 10,000 different numbers?
a hint

Count the whole numbers from 0 to 998.

a worked answer

Only 999 whole numbers lie from 0 to 998, so 1,000 different ones reach 999 or more. That is at least three digits: at least 3 passes and 3,000 placings, exactly that for the numbers 0 to 999. 10,000 different numbers need at least four digits, at least 40,000 placings. Numbered from 0, ten times the numbers take more than ten times the placings.

this tests best, worst and average, on the learn page

what a full answer includes
  • Only 999 whole numbers lie below 999.
  • At least 3 passes and 3,000 placings.
  • At least 4 passes and 40,000 placings.