The heap drawn was made by putting in 16, 29, 23, 47, 35, 40 and 31. Then 23 changes to 50. At which place does 50 stop?
the answer, and why
the answer is B
23 stands at place 3, over 40 and 31. 50 is bigger than both, so it swaps with the smaller, 31, down to place 7, which has no child.
step 16 of 22
23 stands in place 3. The heap keeps a note of where each number stands, so this is one look and no comparison.
comparisons: 0
step 20 of 22
Swap them: 50 sinks below 31.
comparisons: 2
step 21 of 22
Nothing is below 50, so it stays.
comparisons: 2
23 at place 3 becomes 50, sinks below 31, the smaller child, and stops. 2 comparisons.
if you chose another
AThat swaps with 40, the first child. A sink swaps with the smaller child, 31.
CThat leaves 50 over 40 and 31, both smaller than it.
DPlace 2 is not below place 3. Place 3's children are places 6 and 7.
2question 2
On an empty heap, put 6, 7, 8, 9 and 10, change 10 to 1, then change 9 to 2. How many comparisons does the site count?
the answer, and why
the answer is D
The puts compare 4 times. 1 climbs from place 5 to the top, 2 comparisons. 2 climbs from place 4 above 6, then stops under 1, 2 more.
after the puts; 10 at place 5 becomes 1 and climbs 2 levels
after that; 9 at place 4 becomes 2, climbs above 6 and stops under 1
The heap after the puts, then after 10 becomes 1. In all, 4 + 2 + 2 = 8.
if you chose another
AThat counts the choices of climb or sink, 1 against 10 and 2 against 9. They compare new with old, so the site leaves them out.
BThat counts the puts only. The changes climb too.
CThat leaves out the comparison that stops 2 under 1.
3question 3
Each list puts 11 to 17 into an empty heap, smallest first, then makes one change; 11->20 means change 11 to 20. Which list makes the most comparisons?
the answer, and why
the answer is A
With 11->20, the top made 20 sinks two levels, comparing both children at each, 4 comparisons; the others cost 2. With the puts' 6, 11->20 costs 6 + 4 = 10, the others 6 + 2 = 8.
B17 is at the last place. Made 1, it climbs two levels, one comparison each.
C14 is at place 4. Made 2, it climbs two levels too, one comparison each.
D13 is at place 3. Made 30, it sinks one level, then stops at place 6, which has no child.
4question 4
A route search puts 1,000 distances in, smallest first, then makes 1,000 changes, each picking a distance at random and making it the smallest. Which way of keeping them makes the fewest comparisons and looks?
the answer, and why
the answer is C
With a note, finding is one read. A change made the smallest climbs about 8 levels on average, 7,987 for the changes alone (8,986 with the puts' 999), at most 9,000.
AWith no note, finding alone averages 500.5 looks a change, 500,500 in all.
BIt finds by the same looks, 500.5 on average.
DHalving finds it in few looks, but it then walks to the front, 499.5 steps a change on average.
pen and paper
5question 5about 10 minutes
Put 4, 5 and 6 into an empty heap, smallest first. Then make three changes, to 3, then 2, then 1, each picking the number it changes at random.
What does a change cost at places 1, 2 and 3?
Why does each change cost the same on average, whatever came before?
What does the whole job cost on average? Check it against section 4 of the learn page.
a hint
Each change makes its number the smallest, so it climbs to the top from wherever it stands.
a worked answer
Place 1 costs 0, places 2 and 3 cost 1 each. Each place is as likely at every change, so a change averages 2 over 3. With the puts' 2, the job averages 2 + 3 × 2/3 = 4. Section 4's formula gives 2 + 0 + 1 + 1, also 4.
place
levels to the top
picked
1
0
1 time in 3
2
1
1 time in 3
3
1
1 time in 3
a change on average
2 over 3
The heap of 3, and what a change made the smallest costs at each place.
Each change picks a number at random and makes it the smallest. Why do such changes cost on average?21 of 21 shown
1the move: every place as likely
the line:
first change
second change
comparisons
the top
the top
1
the top
place 2
2
place 2
the top
2
place 2
place 2
3
average
2
Put 3 and 4, then two changes, each picking the top or place 2, so 4 picks, each as likely.
why it is allowed: Each number held is as likely, and each has a place of its own, so each place is as likely, whatever the changes before did.
2the move: add up the places
the line:
place
levels to the top
1
0
2
1
3
1
4
2
in all
4
A heap of 4. A change averages 4 times a quarter, which is 1, and four changes give 4.
why it is allowed: is the average comparisons, and the climb from place . The puts make . By line 1, a change averages each climb times . Averages add, by line 2 of question 2 of the page expected cost, so changes give times that.
guess it, then
3the move: cancel
the line:
why it is allowed: By line 2, as times is 1. For 4, 3 + 0 + 1 + 1 + 2 is 7, the average over all 256 picks.
guess it, then
4the move: name the levels
the line:
why it is allowed: By line 2 of the question on the dearest job, is place 's levels, by line 7 of that question.
guess it, then
5the move: the changes alone
the line:
why it is allowed: By line 4, taking away the puts' . For 4, 4.
guess it, then
6the move: count the swaps
the line:
why it is allowed: By line 5, the changes compare that many times. Each comparison of a change made the smallest swaps, by line 2 of the question on the dearest job, and no put does. For 4, 4.
guess it, then
7the move: count level by level
the line:
1 down or more, 6 places
2 down or more, 4 places
A heap of 7, each place showing its levels, 10 in all.
place
1 down or more
2 down or more
levels
1
0
0
0
2
1
0
1
3
1
0
1
4
1
1
2
5
1
1
2
6
1
1
2
7
1
1
2
in all
6
4
10
Each row adds to its place's levels, and the columns give the brackets, 6 and 4.
why it is allowed: By line 4, counting each place once for each level it lies below the top. Level starts at place 2 to the power . Lines 5 and 7 of the box on halving, inside the box under line 4 of the question on the dearest job, show it with for . So the places at least down run from there to .
guess it, then
8the move: gather
the line:
why it is allowed: Each of the brackets holds , less a power of 2. For 4, is 2, and 3 + 2 × 5, less 2 + 4, is 7.
guess it, then
9the move: add 1 and take it away
the line:
why it is allowed: This changes nothing, and the bracket now starts at 1. For 4, 2 + 4 is 1 + 2 + 4, less 1.
guess it, then
10the move: use the doubling sum, h for n
the line:
where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total?
1the move: name the sum
the line:
why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
2the move: double both sides
the line:
why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
3the move: take line 1 away
the line:
why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
4the move: tidy the left side
the line:
why it is allowed: Two lots of less one lot is one lot.
5the move: cancel what both brackets share
the line:
when n is 3
1
2
4
8
16
first bracket
2
4
8
16
second bracket
1
2
4
8
left over
take away 1
16
Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.
why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
6the move: the terms before the last
the line:
why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
7the move: tidy the power
the line:
why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
8the move: read it
the line:
why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under , twice the last.
why it is allowed: The box under this line shows a doubling sum from 1 is one less than its next term. For 4, 1 + 2 + 4 is 7, one less than 8.
guess it, then
11the move: take away its 1
the line:
why it is allowed: One less, then 1 less again, is two less. For 4, 2 + 4 is 6.
guess it, then
12the move: tidy
the line:
why it is allowed: Taking away 2 less than a number takes the number and gives 2 back. For 4, 3 + 10 + 2 less 8 is 7.
guess it, then
13the move: a ceiling from the dearest job
the line:
why it is allowed: No pick costs more than the dearest job, by line 8 of the question on it, and line 11 there caps that. So neither does the average.
guess it, then
14the move: read it as a bound
the line:
why it is allowed: By line 13, with the fixed multiple 2.
guess it, then
15the move: the last level starts in the row
the line:
why it is allowed: Now a floor, above a fixed part of . By line 12. is at most , by line 5 of the box on halving inside the box under line 4 of the question on the dearest job. So is at most , and taking away takes at least as much. At 6, is 13 and the right side 9.
guess it, then
16the move: gather
the line:
why it is allowed: , less , plus 2, leaves 1 minus . At 6, 7 × 2, less 6, plus 1, is 9.
guess it, then
17the move: drop what is left over
the line:
why it is allowed: Line 16 is plus , which is more than 0. At 6, 6 × 1 + 3 is 9.
guess it, then
18the move: a quarter of h + 1
the line:
why it is allowed: From 4 on, is at least 2, and a quarter of is at most . With at 2, three quarters against 1; each 1 more on adds a quarter to the first and 1 to the second.
guess it, then
19the move: a simpler floor
the line:
why it is allowed: is less than . At 4, lines 19, 18 and 17 give 2, 3 and 4, and is 7.
guess it, then
20the move: read it as a bound
the line:
why it is allowed: By line 19, with the fixed multiple a quarter.
guess it, then
21the move: ceiling and floor together
the line:
why it is allowed: Lines 13 and 19 bound by the same growth, , with the fixed multiples 2 and a quarter.
guess it, then
try it on small inputs: over every pick of 4 changes, 256 in all, the average is
A heap with a note of places holds 31 numbers, a full tree of 5 levels, numbered 0 to 4.
At most how many comparisons can one change that makes a number smaller cost?
And one that makes a number bigger?
With no note, how many looks find a number at random, on average?
a hint
Halve 31 again and again, rounding down, until you reach 1.
a worked answer
31 halves to 15, 7, 3 and 1, four halvings, so place 31 lies 4 levels down. Made smaller, a change costs at most 4, one comparison a level. Made bigger, at most 4 rounds of 2, which is 8; in a full tree the top made the biggest reaches it. With no note, a number at random costs 32 over 2, 16 looks.
31 halved, rounding down, reaches 1 in 4 steps, so place 31 is 4 levels down.
One change that makes a number bigger. At most how many comparisons?5 of 5 shown
1the move: a number made bigger sinks
the line:
why it is allowed: It got no smaller, so the number above it is still no bigger; only a number below can be smaller. The counted job makes no such change.
2the move: price a round
the line:
step 22 of 31
8 stands in place 1. The heap keeps a note of where each number stands, so this is one look and no comparison.
comparisons: 0
step 26 of 31
Swap them: 90 sinks below 19.
comparisons: 2
step 29 of 31
Swap them: 90 sinks below 26.
comparisons: 4
step 30 of 31
Nothing is below 90, so it stays.
comparisons: 4
The watch page's second example. 8 at the top becomes 90, sinks below 19, then 26, and stops at place 6. 4 comparisons, 2 swaps.
round
compares
then
1
33 with 90, 19 with 33
swap 90 with 19
2
26 with 90, 52 with 26
swap 90 with 26
no round
no child below 90
it stays
Each round of that change. Two rounds of 2, then none.
why it is allowed: The number itself is the smaller so far at first. So a round makes at most 2 comparisons.
guess it, then
3the move: count the rounds
the line:
why it is allowed: is a ceiling. A round needs a child below, so a sink has at most the last place's levels, by line 4 of the question on the dearest job. Each costs at most 2, by line 2. For 7, 2 × 2 is 4, which the second example reaches.
guess it, then
4the move: drop the rounding
the line:
why it is allowed: By line 3, as rounding down never makes a number bigger.
guess it, then
5the move: read it as a bound
the line:
why it is allowed: By line 4, with the fixed multiple 2.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand