Skip to content

Counting sort

ordering

Level 2 of 3

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

Counting sort works on 3, 0, 2, 3, 1, 0. Once the totals are made, what is the total for 2?

30231001234567890000000000
the answer, and why

the answer is D

The tallies for 0, 1 and 2 are 2, 1 and 1. Added up to 2, that is 4.

if you chose another

  • That is the tally for 2 alone.
  • That is the total for 1.
  • That counts every number.

question 2

How many totals does counting sort add on 7, 2, 7, 5, 2, 9, 0, 7?

7275290701234567890000000000
the answer, and why

the answer is B

One for each value from 1 to 9. The total for 0 is its own tally.

if you chose another

  • That adds to the total for 0 too.
  • That counts only the values in the row.
  • That is how many numbers the row holds.

question 3

Which row makes counting sort place the most numbers?

the answer, and why

the answer is C

Every row places one number for each it holds. The nine 0s are the most numbers.

if you chose another

  • Eight numbers, eight placings, whatever their order.
  • Seven numbers, seven placings.
  • Six numbers, six placings; repeats change nothing.

question 4

Which is true of counting sort on numbers from 0 to 9?

the answer, and why

the answer is D

How many steps it takes never depends on the order, only on how many numbers the row holds.

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

if you chose another

  • The order never changes how many steps it takes.
  • 2n+92n + 9 grows like nn, no faster.
  • It compares nothing, so the sorting floor is not about it.

pen and paper

question 5about 12 minutes

Counting sort works on 5, 1, 4, 1, 5, 9, 2, 6.

  1. Write the tallies, then the totals.
  2. Send each number from the back, and write the row it makes.
  3. Count the counts, additions and placings, and check them against 2n+92n + 9.
a hint

The total for a value is the place its last copy goes to.

a worked answer

Tallies for 0 to 9: 0, 2, 1, 0, 1, 2, 1, 0, 0, 1. Totals: 0, 2, 3, 3, 4, 6, 7, 7, 7, 8. From the back, 6 goes to place 7, 2 to place 3, 9 to place 8 and 5 to place 6. Then 1 goes to place 2, 4 to place 4, 1 to place 1 and 5 to place 5. The row is 1, 1, 2, 4, 5, 5, 6, 9. Steps: 8 counts, 9 additions and 8 placings, 25 in all, and 2 times 8, plus 9, is 25.

How many counts, additions and placings in all, and how do they grow?
  1. the move: name every step

    the line:
    every step is a count,an addition or a placing\begin{gathered} \text{every step is a count,} \\ \text{an addition or a placing} \end{gathered}

    why it is allowed: TT counts these three kinds of step. Setting the ten tallies to 0 at the start is a fixed 10 more, left out, and the last moment only says the row is in order.

  2. the move: put in the three counts

    the line:
    T=n+9+nT = n + 9 + n

    why it is allowed: By line 1, with the counts worked out in the learn page's section 4, nn counts, 9 additions and nn placings.

  3. the move: gather

    the line:
    T=2n+9T = 2n + 9
    numberscountedadditionsplacedsteps
    449417
    889825
    121291233
    Every step for 4, 8 and 12 numbers. The 9 additions stay the same; the rest grow with the row.

    why it is allowed: By line 2, nn and nn make 2n2n. The table tries 4, 8 and 12 numbers.

  4. the move: a ceiling

    the line:
    T≤2n+9nT \le 2n + 9n

    why it is allowed: In line 3, from 1 number on, 9 is at most 9 times nn.

  5. the move: gather

    the line:
    T≤11nT \le 11n

    why it is allowed: By line 4, 2 times nn plus 9 times nn is 11n11n. At 1 number both sides are 11.

  6. the move: read it as a bound

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

    why it is allowed: By line 5, TT is at most 11 times nn.

  7. the move: a floor

    the line:
    T≥2nT \ge 2n

    why it is allowed: In line 3, leaving out the 9 leaves less.

  8. the move: read it as a bound

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

    why it is allowed: By line 7, TT never drops below 2 times nn.

  9. the move: ceiling and floor together

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

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

try it with a small nn: at n=4n = 4, line 2 gives T=17T = 17, which you can work out by hand

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

what a full answer includes
  • The tallies and totals.
  • The eight sends and the row.
  • The 25 steps matching the formula.

question 6about 8 minutes

Now say the numbers may be any whole numbers from 0 to 999.

  1. How many totals are added?
  2. How many counts, additions and placings for 12 numbers?
  3. Merge sort makes at most 33 comparisons on 12 numbers, on its worst row. Which would you use, and when?
a hint

There is a tally for every value, used or not.

a worked answer

The total for 0 is its own tally, and each value from 1 to 999 adds one: 999 additions. For 12 numbers, 12 counts, 999 additions and 12 placings, 1,023 steps. Merge sort's 33 comparisons are far fewer. On 12 numbers, 12 counts and 12 placings make 24, and the additions are one fewer than the possible values. That is 28 steps for values from 0 to 4, and 33 for 0 to 9. So counting sort takes fewer steps than merge sort's 33 comparisons only with fewer than 10 values. Merge sort also places 44 numbers on that row, 12, 12, 8 and 12 in its four rounds; counted alike, 77 steps, counting sort wins with up to 53 values.

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

what a full answer includes
  • 999 additions.
  • 1,023 steps.
  • Under 33 steps only with fewer than 10 values.