Every position past ⌊n/2⌋ is a leaf, and a leaf is a one-element heap already. So a heap can be built by sifting down at every position from ⌊n/2⌋ back to 1, in that order. The order is what makes it correct: when the pass reaches node i, everything below i has already been processed, so both subtrees of i are heaps and a single sift-down from i puts i where it belongs.
Definition · Floyd's build-heapGiven an array of n keys in any order, for i from ⌊n/2⌋ down to 1, sift down at position i. The result is a heap, and the cost is the total distance sifted.
The loop touches only the internal nodes, since leaves have nothing to sift past. The bound is about the total distance, so it needs the number of nodes at each height and not merely the height of the tree.
Theorem · Floyd's build-heap costs at most 2nA node at height h sifts at most h levels, and a complete tree on n nodes has at most ⌈n/2ʰ⁺¹⌉ nodes at height h. So the total distance sifted is at most Σ ⌈n/2ʰ⁺¹⌉·h, summed over h = 0 to ⌊log₂ n⌋.
Dropping the ceilings, that is at most n · Σ h/2ʰ⁺¹, and Σ h/2ʰ over all h ≥ 0 converges to 2. So the sum is under 2n, and the build is Θ(n): linear in the number of keys, with no logarithm anywhere in it.
The Σ h/2ʰ = 2 step is the only piece of analysis being imported, and it is the standard arithmetico-geometric sum — differentiate the geometric series, or notice that the sum S satisfies S = S/2 + 1. Discrete Mathematics’ “Recursion Trees and Amortised Analysis” is where sums of this shape are collected; nothing else here needs it.
The sum, evaluated exactly
The lab evaluates the sum term by term rather than quoting the 2n bound, because the terms are where the insight is. At n = 31 the complete tree has five levels and the nodes are distributed like this:
height h nodes ⌈31/2ʰ⁺¹⌉ h · nodes
0 16 0 the leaves sift nowhere
1 8 8
2 4 8
3 2 6
4 1 4 the root, alone
total 26 and 2n = 62
per element 26/31 under one swap each
Sixteen of the thirty-one nodes contribute nothing at all, and the two heights that contribute most are 1 and 2 — not the height of the tree. The total is 26, which is 26/31 of a swap per element. Compare that with what n log₂ n would have suggested: 31 × 4 is 124, nearly five times as much, and the error is entirely in charging every node the root’s height.
Both methods, on the same input
Now the measurement. Feed both methods the keys 31, 30, …, 1 — decreasing order, which is the worst case for insertion because every arriving key is smaller than everything present and sifts to the root:
n = 31, keys arriving in decreasing order
swaps comparisons
Floyd's build 26 52
n inserts one at a time 98 98
exact sum Σ ⌈n/2ʰ⁺¹⌉·h 26 the bound Floyd's swaps hit
same keys, arriving in increasing order
Floyd's build 0 30
n inserts one at a time 0 30 identical
Floyd’s swap count is 26, which is the sum exactly: this input makes every internal node sift its full height, so the bound is attained rather than merely respected. Insertion swaps 98 times on the same keys, a factor of 3.8. And on keys already in increasing order both methods swap zero times and make the same thirty comparisons, producing the same heap — a measurement taken there would show no difference between a linear algorithm and a linearithmic one.
Two things in that table are worth being careful about. First, the exact sum bounds the swaps, not the comparisons: Floyd’s comparisons are 52, about twice its swaps, because each level of a sift-down compares the two children before comparing with the parent. The 2n bound is a bound on distance sifted. Second, on a shuffled input Floyd swaps 21 — below the sum, because a random arrangement does not make every node sift its full height. The sum is the worst case; the counter shows one case.