Heap-Build Methods.(a) In the top-down heap-build method, we use Max-Heap-Insert repeatedly, starting with an empty heap and inserting from position 1 to n, resolving the heap structure by “bubbling up” each new insertion. This means the 2i nodes on level i will each cause up to i swaps, resulting in the following expression:total # swaps = Σ(# nodes)(#swaps)≤ 1 · 0 + 2 · 1 + 22 · 2 + . . . + 2i · i + . . . + n2 · log nYour task: Argue that this expression is Θ(n log n).Hint: Lower bound is easy. For the upper bound, exaggerate the amount of bubbling up to be log n per node.
(b) In the bottom-up heap-build method, we use MAX-HEAPIFY repeatedly from po- sition n to 1, resolving the heap structure by "bubbling down" from each position in turn; however, the first ~ ? positions are leaves and thus already heaps. This means the 2i nodes on level i will each cause up to log(n) -- i swaps, resulting in the following expression:
total # swaps 1.log n+2.((log n)-1)+22.((log n)-2)+...+2.((log n)-i)+...
Note: Really the last nodes have 0 swaps but this expression is simplified since the actual height is [log n]. Regardless, this is a fine upper bound.
Reversing the order of the terms,this can be rewritten:
n total # swaps 2
n
n
n . 4+...+2. ((log n) -1)+1.log n 16 (log n)
8 3
2
4
22
23
n
Your task: Argue that the above parenthetical expression is bounded above by a constant, thus the expression is O(n). Hint: One method is the following. First, bound above by the corresponding infinite
to do next.