How many comparisons does the sort make lining up the letters of deadbeef, the earlier letter first on a tie?
the answer, and why
the answer is D
In letter order a 1, b 1, d 2, e 3, f 1; on a tie, the earlier letter goes first. Round 1: a with b and d with e, 1 each. Round 2: a, b with d, e takes 2, as both of the right run weigh more. Round 3: a, b, d, e with f takes 3, f going after a and b. That is 7.
if you chose another
AThat counts the 8 letters of the text, but the sort lines up the 5 different letters.
BThat is the joins, one fewer than the 5 letters.
CThat counts the different letters, not the comparisons between them.
2question 2
How many joins does the method make on committee?
the answer, and why
the answer is A
Committee has 6 different letters, c, e, i, m, o and t. Each join leaves one tree fewer, from 6 to 1.
if you chose another
BThat counts the different letters; the first join already leaves 5 trees.
CThat counts the letters of the text.
DThat is the comparisons the sort makes.
3question 3
A word's letters weigh 1, 2, 3 and 5. The method's joins weigh 3, 6 and 11. How many bits does the word take, each letter's code written every time it appears?
the answer, and why
the answer is B
The bits are the joins' weights added up, 3 + 6 + 11. Check by depths. The 5 is 1 deep, the 3 is 2 deep, and the 1 and 2 are 3 deep. 5 + 6 + 3 + 6 is 20.
AThat is the last join alone, every letter counted once.
CThat adds each code's length once, 1 + 2 + 3 + 3, not times its weight.
DThat counts the 11 letters again on top of the joins' 20.
4question 4
Which word makes the sort compare the most?
the answer, and why
the answer is B
Its weights in letter order are 1, 3, 2, 4, the dearest order of 4. Round 1 makes 1 comparison in each of its 2 merges, and round 2 makes 3, so 5.
if you chose another
AWeights falling, 4, 3, 2, 1, the fewest. 1 in each merge of round 1, then 2, so 4.
CWeights 1, 2, 3, 4, already in order. 1 in each merge of round 1, then 2, so 4.
DAll weigh 2, and ties go in letter order, already in order. 4.
pen and paper
5question 5about 8 minutes
Run the method by hand on peppers, keeping the two queues, one of letters and one of joins.
Count the letters and line them up.
Make each join, saying which queue each tree comes from.
Without reading off the codes, give the bits from the joins alone. Then say which two letters share the deepest join, and why.
a hint
Take the lighter front of the two queues each time, and the letter on a tie.
a worked answer
p 3, e 2, r 1, s 1, lined up r, s, e, p. J1 joins r and s, both from the letters, weighing 2. J2: e and J1 both weigh 2, and the letter goes first; then J1, at 2, is lighter than p, at 3. J2 weighs 4. J3 joins p, a letter, and J2: 7. The bits are the joins added up, 2 + 4 + 7, which is 13. J1 holds r and s, J2 holds J1, J3 holds J2. So r and s sit under all 3 joins, 3 deep; e is 2 deep, p 1.
join
takes
from
weighs
J1
r 1 and s 1
letters, letters
2
J2
e 2 and J1 2
letters, joins
4
J3
p 3 and J2 4
letters, joins
7
The joins on peppers. At J2, e and J1 both weigh 2, and the letter is taken first.
13 bits, 2 + 4 + 7; r and s, under all 3 joins, 3 deep.
6question 6about 8 minutes
A program builds a code for 1,024 different letters with the watch page's method.
How many comparisons with weights falling?
How many with weights in the dearest order?
How many joins, whatever the weights?
a hint
1,024 is ten 2s multiplied together, so no run ever waits. Questions 1 to 3 of the learn page's section 4 count each.
a worked answer
No run waits, and each of the 10 rounds' right runs hold half the letters, 512. With weights falling, each right run is used up first: 10 times 512, which is 5,120. In the dearest order, 1,024 times 10, less 0 waiting, less 1,023 for the merges: 9,217. The joins are 1,023 either way.
Question 1: how many comparisons, , does the sort make on letters with weights falling in letter order, from 2 up?11 of 11 shown
1the move: how the sort goes
the line:
5 numbers stand for 5 letters' weights, falling. Round 2's merge, 4, 5 with 2, 3, compares 2 with 4, then 3 with 4. Then the right run is used up, and 4 and 5 follow with no comparison.
round
run length
merges
comparisons
1
1
2
2
2
2
1
2
3
4
1
1
all
4
5
Five letters, weights falling. In rounds 1 and 2 the last run waits.
why it is allowed: The watch page's sort is merge sort, from the bottom up. Each letter starts as a run of 1, in letter order. Each round merges neighbouring runs, comparing their front letters and taking the lighter. A last run with no partner waits. Every run is 1, 2, 4 and on long, except perhaps a round's last, which is shorter. A last run is a right run or waits, so a right run is never the longer. Which runs meet depends only on , never on the weights.
2the move: a merge's least
the line:
why it is allowed: A merge stops when one run is used up, and each letter of that run needed one comparison to leave it. By line 1, the right run is never the longer. So whichever run is used up holds at least the right run's length. For 4, 5 with 2, 3, at least 2 comparisons.
guess it, then
3the move: weights falling, the right run is used up first
the line:
run length
right runs
comparisons
1
5 of 1
5
2
2 of 2
4
4
1 of 4
4
8
1 of 2
2
all
15
Ten letters, weights falling. At run length 2 the last run waits, and at 4 the last 2 letters wait. At 8 the right run holds only those 2.
why it is allowed: With weights falling, each right run's letters weigh less than the left run's beside it. So the right run is used up first, one comparison each, the least line 2 allows. adds up the right runs in the round of runs of 1, of runs of 2, and so on. For 5, 2 + 2 + 1.
guess it, then
4the move: a ceiling
the line:
Top, numbers, each double the last; bottom, their logs, each 1 more. 5 lies between 4 and 8, so its log, about 2.3, rounds up to 3. 10 lies between 8 and 16, so its log, about 3.3, rounds up to 4.
why it is allowed: In a pair the right run is never the longer, so a round's right runs hold at most half the letters, . Run lengths double until one run holds all , so there are rounds. Multiply the two. For 5, half of 5 times 3 rounds is 7.5, and is 5.
guess it, then
5the move: a simpler ceiling
the line:
why it is allowed: Rounding up adds less than 1, and when is 2 or more, is at least 1. So is at most twice , and times twice is . For 5, is 5, at most 11.6.
guess it, then
6the move: read the bound
the line:
why it is allowed: By line 5, with the fixed multiple 1.
guess it, then
7the move: a floor
the line:
run length
full pairs hold
their right runs
a quarter of 5
1
4
2
1.25
2
4
2
1.25
Five letters. In each round with a full pair, the full pairs hold 4 of the 5, at least half, and their right runs 2, at least a quarter. 2 such rounds, so at least 2.5.
why it is allowed: Take a round whose runs are long, with at most , so at least one full pair of runs fits, letters. What is left over is fewer than , so the full pairs hold at least half the letters, and their right runs at least a quarter, . Such rounds have as 1, 2, 4 and on, while is at most , so there are of them. By line 3, adds up every round's right runs, so multiply the two. For 5, rounds of 1 and 2, so at least 2.5.
guess it, then
8the move: the rounded log against the exact one
the line:
why it is allowed: Call line 7's rounded-down log . Rounding down took off less than 1, so is below . From 2 up is at least 1, so is at most . For 5, is 2, and the log of 5, about 2.3, is below 3, and 3 is at most 4.
guess it, then
9the move: drop the rounding
the line:
why it is allowed: By line 8, is at least half of . Put that into line 7, and times half of is . For 5, is 5, at least 1.45.
guess it, then
10the move: read the bound
the line:
why it is allowed: By line 9, with the fixed multiple an eighth.
guess it, then
11the move: both together
the line:
why it is allowed: Lines 6 and 10 bound by the same growth.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand