Académique Documents
Professionnel Documents
Culture Documents
• T (n) is O(n log2 n). To show this upper bound on T (n), note that on every input array A of size
n the loop of Build-by-Inserts is executed exactly n − 1 times. Each time this loop is executed,
it inserts an element into a heap that has at most n elements. Thus, there is a constant c such
that every insertion takes at most c log2 n time, and so the n − 1 iterations of the loop take at most
cn log2 n time. We conclude that T (n) is O(n log2 n).
• T (n) is Ω(n log2 n). To show this lower bound on T (n), consider the execution of Build-by-Inserts
on an input array A of n elements such that the n elements of A appear in increasing order. In this
execution, for each j:
1. The jth iteration of the loop inserts an element to a heap that already contains j elements.
2. The jth iteration of the loop inserts an element that is larger than any other presently in the
heap, and so this element must “percolate up” from the bottom level to the root of the heap.
So there is a constant c such that forPevery j, the jth iteration takes at least cblog2 jc time. Thus,
n−1
the n − 1 iterations take at least c j=1 blog2 jc time. It turns out that, for all n ≥ 28 = 256,
Pn−1 1
j=1 blog2 jc ≥ 16 n log2 n (we include the proof at the end of this solution set). We conclude that,
for every n ≥ 256, there is an input array A of length n for which BuildNewHeap takes at least
c
16 n log2 n time. Thus, T (n) is Ω(n log2 n).
1
Answer to Question 3. (20 marks)
a. (12 marks) Describe your algorithm in clear and concise English.
2
Let S(n) = n−1 1
P
j=1 blog2 jc. In Question 1 (b), we claimed that for all n ≥ 256, S(n) ≥ 16 n log2 n. We now
prove this result.
We have: S(n) ≥ n−1
P Pn−1 Pn
j=1 (log2 j − 1) ≥ ( j=1 log2 j) − n = ( j=1 log2 j) − log2 n − n.