Heap sort works on seven equal numbers, 5, 5, 5, 5, 5, 5, 5.
Count the comparisons that build the heap.
Count those that sort it.
Check the total against .
a hint
Every sift stops after one round.
a worked answer
Building: places 3, 2 and 1, 2 comparisons each, 6. Heap of 6: 2. Heap of 5: 2. Heap of 4: 2. Heap of 3: 2. Heap of 2: 1. Sorting makes 9. In all 15, and 3 times 7, less 6, is 15.
Why do equal numbers cost exactly comparisons, the fewest any row can?12 of 12 shown
1the move: one round each
the line:
sift
place
its children
comparisons
building
2
4 and 5
2
building
1
2 and 3
2
heap of 4
1
2 and 3
2
heap of 3
1
2 and 3
2
heap of 2
1
2
1
in all
9
Heap sort on 8, 8, 8, 8, 8, sift by sift. 4 comparisons build the heap, and 5 sort it.
why it is allowed: With equal numbers no child is bigger, so every sift stops after its first round. The picture counts 8, 8, 8, 8, 8.
2the move: count the building
the line:
why it is allowed: Each place with a child is sifted once, and every place but place 1 is the child of one place. So comparisons; 4 for 5 numbers.
guess it, then
3the move: count the sorting
the line:
why it is allowed: The tops of heaps of numbers down to 2 are sifted. The heaps of 3 or more give their top 2 children, as the heaps of 4 and 3 in the picture. The heap of 2 gives 1.
guess it, then
4the move: add them
the line:
why it is allowed: is the comparisons on equal numbers, by lines 2 and 3.
guess it, then
5the move: multiply out
the line:
why it is allowed: Twice is .
guess it, then
6the move: gather
the line:
why it is allowed: The 1s cancel. For 5 numbers, 4 + 4 + 1 is 9, as in the picture.
guess it, then
7the move: no row costs fewer
the line:
why it is allowed: On any row, every sift compares at least the children of its place. Lines 2 and 3 count only those.
guess it, then
8the move: a ceiling
the line:
why it is allowed: By line 6, taking 6 away.
guess it, then
9the move: read it as a bound
the line:
why it is allowed: By line 8, stays under 3 times .
guess it, then
10the move: a floor
the line:
why it is allowed: is plus , and is at least 0 from 3 on. At 5, 9 is 5 + 4.
guess it, then
11the move: read it as a bound
the line:
why it is allowed: By line 10.
guess it, then
12the move: ceiling and floor together
the line:
why it is allowed: Lines 9 and 11 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
Work out at 7, the count no row of 7 numbers goes past.
Work out at 7, the floor under the dearest row.
Put 19 and the dearest row of 7, which costs 22, in order between them.
a hint
At 7, the height is 2 and there are 3 parents. For , find the first power of 2 at or above 5,040, the orders of 7 numbers.
a worked answer
is 2 times 2 times (3 + 7 - 2), so 4 times 8, which is 32. 2 to the 12 is 4,096, below 5,040, and 2 to the 13 is 8,192, so is 13. In order: 13, then this row's 19, then the dearest row's 22, then 32. The dearest row sits between its floor and the ceiling, and this row costs less.