CIt compares nothing, so the sorting floor is not about it.
pen and paper
5question 5about 12 minutes
Counting sort works on 5, 1, 4, 1, 5, 9, 2, 6.
Write the tallies, then the totals.
Send each number from the back, and write the row it makes.
Count the counts, additions and placings, and check them against .
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?9 of 9 shown
1the move: name every step
the line:
why it is allowed: 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.
2the move: put in the three counts
the line:
why it is allowed: By line 1, with the counts worked out in the learn page's section 4, counts, 9 additions and placings.
guess it, then
3the move: gather
the line:
numbers
counted
additions
placed
steps
4
4
9
4
17
8
8
9
8
25
12
12
9
12
33
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, and make . The table tries 4, 8 and 12 numbers.
guess it, then
4the move: a ceiling
the line:
why it is allowed: In line 3, from 1 number on, 9 is at most 9 times .
guess it, then
5the move: gather
the line:
why it is allowed: By line 4, 2 times plus 9 times is . At 1 number both sides are 11.
guess it, then
6the move: read it as a bound
the line:
why it is allowed: By line 5, is at most 11 times .
guess it, then
7the move: a floor
the line:
why it is allowed: In line 3, leaving out the 9 leaves less.
guess it, then
8the move: read it as a bound
the line:
why it is allowed: By line 7, never drops below 2 times .
guess it, then
9the move: ceiling and floor together
the line:
why it is allowed: Lines 6 and 8 bound by the same growth, .
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
Now say the numbers may be any whole numbers from 0 to 999.
How many totals are added?
How many counts, additions and placings for 12 numbers?
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.