A row holds 10 numbers. Which places does building a heap sift, and in what order?
the answer, and why
the answer is C
The last parent is place 5, half of 10, with place 10 its only child. The parents sift from the last back to the first, so each finds both parts below it heaps already.
A row of 10. The marked places 1 to 5 have children; places 6 to 10 have none. They sift 5 first, then 4, 3, 2 and 1.
AThat sifts from the top down, before the parts below place 1 are heaps.
BPlaces 6 to 10 have no children, as twice 6 is 12, past the end. Each is a heap of one already.
DThose are the places with no children, which never sift.
2question 2
On which of these rows does building a heap make no swap at all?
the answer, and why
the answer is A
Every parent is at least as big as its children, 91 over 58 and 73, 58 over 12 and 44, 73 over 60 and 29. So every sift stops at once.
91, 58, 73, 12, 44, 60, 29 as a tree. Each marked parent is at least as big as its children.
if you chose another
B60 at place 5 is bigger than its parent, 58, so 58 swaps down.
C91 at place 2 is bigger than the top, 58, so 58 swaps down.
D60 at place 6 is bigger than its parent, 58, so 58 swaps down.
3question 3
Each row holds the same six numbers. On which does building a heap make the most comparisons?
the answer, and why
the answer is A
27 at the top swaps with 62, then sinks into place 2, which has two children, for another round of 2. That makes 7. The others cost 6, 5 and 6.
sift at place
A
B
C
D
3
1
1
1
1
2
2
2
2
2
1
4
3
2
3
in all
7
6
5
6
Each row's comparisons, sift by sift. Place 3 has one child.
if you chose another
BSmallest first swaps the most, but costs 6. 15 sinks into place 3, which has one child, so its second round compares once.
CThat is a heap already, the cheapest, 5.
D59 sinks into place 3, which has one child, so its second round compares once. 6 in all.
4question 4
Shuffled rows of 1,000 numbers cost about 1,870 comparisons each, on average. About how many does a shuffled row of a million cost?
the answer, and why
the answer is B
From 1,000 on, section 2 of the learn page shows the comparisons growing in step with the numbers, a thousand times as many for a thousand times the numbers. The measured mean at a million is 1,881,312.5.
AThat multiplies by a million, as if the cost grew with the square of the numbers.
CThat grows like . is about 10 at 1,000 and 20 at a million, so 1,870 × 1,000 × 2, about 3.7 million.
DThat multiplies by 2 only, 1,870 × 2, as if the cost grew like . Each of places 2 to a million is met once by its parent, one comparison each, so at least 999,999.
pen and paper
5question 5about 12 minutes
1,023 numbers arrive at once, smallest first. They fill a full tree of 10 levels, 1, 2, 4 and on up to 512.
Put them in one at a time. How many comparisons does that make?
Build bottom up instead. At most how many comparisons, by line 6 of the question on the ceiling?
Compare the two per number. Which way do you choose here?
a hint
One at a time, each number put on level climbs levels to the top. Bottom up, halve 1,023 again and again, rounding down.
a worked answer
Level holds 2 to the power numbers, each climbing . The grid adds the levels: 8,194. Bottom up, line 6 adds 511, 255, 127, 63, 31, 15, 7, 3 and 1, which make 1,013, and doubles it: 2,026. On a full heap a smallest-first row makes exactly that, as line 3 of the question on the ceiling shows. Per number, 8,194 over 1,023 is about 8, and 2,026 over 1,023 about 2. With all 1,023 in hand, build bottom up.
level
numbers
each climbs
comparisons
0
1
0
0
1
2
1
2
2
4
2
8
3
8
3
24
4 to 7
240
4 to 7
1,504
8
256
8
2,048
9
512
9
4,608
in all
1,023
8,194
One at a time, smallest first, level by level.
What count of comparisons can no row of numbers go past?13 of 13 shown
1the move: price a round
the line:
step 12 of 20
Each part below place 1 is a heap already. Sift its number, 46, down: while a child is bigger, swap it with its bigger child.
comparisons: 0
step 15 of 20
Swap 46 down with its bigger child, 85.
comparisons: 2
step 18 of 20
Swap 46 down with its bigger child, 67.
comparisons: 4
step 19 of 20
46 has no children, so it stops here.
comparisons: 4
The sift at place 1 of section 1's row, its comparisons counted from this sift's start. 46 meets two children and swaps below 85, then below 67, and stops at place 4, which has none. 4 comparisons.
why it is allowed: Each child is compared with the bigger so far, the number itself first. So a round makes at most 2 comparisons. The drawing follows 46 in section 1's row.
2the move: count the rounds
the line:
The number in each place is its height in a row of 10. Place 3's part is places 3, 6 and 7, one level deep, so its height is 1. In all 8.
why it is allowed: , the height of place , is the steps from down to the deepest place in its own part. A round needs a child below, so a sift makes at most that many rounds.
guess it, then
3the move: add up every sift
the line:
numbers
costs the most
smallest first
4
4
4
6
5
6
6
6
6
7
6
8
7
8
8
8
8
11
10
14
Every order of 1 to n tried, in comparisons. No order passes , and the cheapest is always .
why it is allowed: is a count no row goes past, by lines 1 and 2, as each sift makes at most 2 comparisons in each of its rounds. For 10, 2 × 8 is 16. On a full heap, like 7 or 15, smallest first makes exactly . Every parent there has two children, and each sifting number is smaller than all below it. The sifts before it only move later places. So it sinks to the bottom: rounds of 2. At 7, 2 + 2 + 4 = 8.
guess it, then
4the move: a step down at least doubles
the line:
steps down from place 3
places reached
in a row of 10
1
6 and 7
yes
2
12 to 15
no
One step down from place 3 reaches places 6 and 7, two steps places 12 to 15, past the end. So place 3 is 1 high.
why it is allowed: Place 's children are and , as the box at line 3 of the question on a heap already shows. So the places steps under start at place .
guess it, then
5the move: count the places
the line:
why it is allowed: Place is in the row when is at most over . A place is a whole number, so at most that rounded down. For 10, at height 2, 10 over 4 is 2.5, so places 1 and 2.
guess it, then
6the move: add the heights level by level
the line:
place
at least 1 high
at least 2 high
at least 3 high
height
1
1
1
1
3
2
1
1
2
3
1
1
4
1
1
5
1
1
in all
5
2
1
8
A row of 10, places 6 to 10 left out, being 0 high. The columns add to 5, 2 and 1, and the heights to 8.
why it is allowed: Count each place once at every height up to its own, by line 5. is the top's height, and no place is higher. For 10, 5 + 2 + 1 is 8.
guess it, then
7the move: drop the rounding
the line:
why it is allowed: Rounding down never makes a number bigger; 2.5 becomes 2. For 10, 5 + 2.5 + 1.25 is 8.75.
guess it, then
8the move: take n out
the line:
why it is allowed: Each term is times a halving term.
guess it, then
9the move: use the halving sum
the line:
where does this come from?
question 2 of the page doubling and halving sumsAdd . How close does it come to 1?
1the move: name the sum
the line:
Line 1 for 3, 7 over 8, short of 1 by 1 over 8.
why it is allowed: Let be the halving sum from one half down to 1 over . The bars add it for 3.
2the move: double both sides
the line:
why it is allowed: As in question 1 of the page doubling and halving sums: double, take away, and all but the ends cancel. Doubled, one half becomes 1, and 1 over becomes 1 over , as is twice . For 3, 2 × 1 over 8 is 2 over 8, which is 1 over 4.
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:
why it is allowed: Every term from one half to 1 over is in both brackets and cancels, leaving 1 less 1 over . For 3, 1 less 1 over 8 is 7 over 8.
6the move: a ceiling
the line:
why it is allowed: Line 5 takes 1 over , above zero, from 1, so is under 1, and so at most 1.
7the move: read it
the line:
why it is allowed: By line 5 the sum falls short of 1 by its last term, which halves each time but never reaches zero. Line 6 makes 1 its ceiling.
why it is allowed: The box under this line adds the halving sum, its being here. It falls short of 1 by its last term. For 10, is 3, and 1 less 1 over 8 is 7 over 8.
guess it, then
10the move: multiply out
the line:
why it is allowed: Twice times each part. For 10, 20 less 2.5 is 17.5.
guess it, then
11the move: the top's path ends in the row
the line:
The top's path down by first children, places 1, 2, 4 and 8, ends inside a row of 10. The small number beside each place counts its steps up from place 8, so the top is 3 high. 2 × 10 over 8 is 2.5, so is at most 17.5, under 18.
why it is allowed: The top is high, so by line 4 place is in the row. So is at most , and is at least 2. Taking away at least 2 leaves at most .
guess it, then
12the move: read it as a bound
the line:
why it is allowed: By line 11, with the fixed multiple 2.
guess it, then
13the move: every row grows in step with n
the line:
why it is allowed: No row goes past , so none past , by line 11, and none costs less than , by the question on a heap already. Both grow in step with . Each round makes at most 2 comparisons and at most 1 swap, so the swaps are at most half of , at most by line 11. For 10, at most 8 swaps, fewer than the 10 numbers.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
At most 2,026, which smallest first makes exactly.
About 8 a number against about 2, so build bottom up.
6question 6about 8 minutes
Building a heap from 88, 26, 71, 45, 19, 64, 53 makes 6 comparisons, the fewest any 7 numbers can.
Why can no row of 7 numbers cost fewer than 6?
This row is no heap, as 26 sits over 45. Why does it still cost only 6?
A build makes comparisons and no swap. Must the row have been a heap already?
a hint
A sift makes a second round only when its number lands on a place with children.
a worked answer
Every sift starts by meeting each child of its place once, and places 2 to 7 are each one parent's child: 6. Here 26 swaps with 45 into place 4, which has no children, so its sift ends with no more comparisons. 71 and 88 stop after their first round. Yes: with no swap, every sift stopped after its first round, so each parent was at least as big as its children.
26 at place 2 meets its children, 45 and 19, and swaps with 45 down to place 4. Place 4 is a leaf, with no children, so the sift ends with no second round.
Why does a row that is a heap already cost exactly comparisons, the fewest any row can?10 of 10 shown
1the move: a sift's first round
the line:
A heap already, 92, 74, 81, 33, 60, 18, 47. Each marked parent is at least as big as its children.
sift at place
its children
comparisons
3
18 and 47
2
2
33 and 60
2
1
74 and 81
2
in all
6
Each parent meets its two children, then stops. At place 1, 74 against 92 leaves 92 the bigger so far, and so does 81 against 92; 2 comparisons. 6 in all, no swap.
why it is allowed: Each child is compared once with the bigger so far. In a heap already no child is bigger, so every sift stops after that round, and nothing moves.
2the move: add up the first rounds
the line:
why it is allowed: is the comparisons on a heap already. Place has a child when place is in the row, so when is at most half of . is whole, so the parents are places 1 to . At 7, 2 × 3 = 6 is in the row and 2 × 4 = 8 is not, so places 1 to 3, as section 1's picture marks. A parent has two children when is in the row too, else one.
guess it, then
3the move: every place but the top is a child
the line:
where does this come from?
question 2 of the page trees and their wordsFrom place of a tree kept in a row, which places lie one step down, and which one step up?
1the move: find where a level starts
the line:
The tree of 10 places. Its levels start at places 1, 2, 4 and 8, each block in the row starting where the one before ends.
why it is allowed: Say place is on level . No level starts until the one above it is used up, so the levels above level are full. For level 3, levels 0 to 2 hold 1, 2 and 4, which is 7 places, so level 3 starts at place 8. In general the levels above form a full tree whose height is one less than . By question 1 of the page trees and their words it holds one place fewer than , so level starts at place . Level 0 is the root alone, at place 1, and 2 to the power 0 is 1.
2the move: count what comes before the first child
the line:
Places 2 and 3 take their children in turn, 4 and 5, then 6 and 7. So the first child of place 3 comes after the two of place 2.
why it is allowed: Let be the place of the first child of . On level , places come before . The row fills level from its left end, each place's children in turn, so each place before has its two children there before 's first. That level starts at place . For place 3, one place comes before it on level 1, so its first child is 4 + 2, place 6.
3the move: multiply out
the line:
why it is allowed: Twice is twice less twice .
4the move: tidy the power
the line:
why it is allowed: Twice is .
5the move: cancel
the line:
why it is allowed: The first and last terms cancel. For place 3, twice 3 is 6, as line 2 found.
6the move: the second child is next
the line:
why it is allowed: The two children stand side by side. For place 3, places 6 and 7. A place whose first child would fall past the end of the row is a leaf.
7the move: halve a first child
the line:
why it is allowed: The floor brackets round down, as on the page on halving. Half of is exactly, so rounding changes nothing.
8the move: halve a second child
the line:
why it is allowed: Half of is and a half, which rounds down to .
9the move: read it
the line:
why it is allowed: Every place but the root is the first or the second child of some place, twice it or one more, by lines 5 and 6. Halving it and rounding down gives that place back, by lines 7 and 8. For places 6 and 7, place 3.
why it is allowed: The box under this line shows each place but the top has exactly one place above it. So places 2 to are each counted once. For 7, 2 + 2 + 2 is 6; for 6, place 3 has one child, so 2 + 2 + 1 is 5.
guess it, then
4the move: no row costs fewer
the line:
why it is allowed: On any row, each sift starts by meeting its place's children, so no row costs less than . A row of 7, 7, 7, 7, 7, 7, 7 costs exactly 6, as no child is bigger.
guess it, then
5the move: which rows cost that
the line:
why it is allowed: With no swap, every sift stopped at once, so each parent was at least as big as its children. A swap into a place with no children adds no comparison, so some rows with swaps cost too. In the row 88, 26, 71, 45, 19, 64, 53, the 26 swaps into place 4, which has no children, and the row costs 6.
guess it, then
6the move: a ceiling
the line:
why it is allowed: By line 3, taking 1 away.
guess it, then
7the move: read it as a bound
the line:
why it is allowed: By line 6, with the fixed multiple 1.
guess it, then
8the move: a floor
the line:
why it is allowed: Half of is at most from 2 on. At 7, 3.5 is under 6.
guess it, then
9the move: read it as a bound
the line:
why it is allowed: By line 8, with the fixed multiple a half.
guess it, then
10the 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