

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Ruan; Class: Analysis Of Algorithms; Subject: Computer Science; University: University of Texas - San Antonio; Term: Spring 2008;
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


a. (15 points) Study the pseudocode of BuildHeap and Heapify on Slide #11 and #23 in Lecture 13. Use Slides #24 – 36 as a model, illustrate the operation of BuildHeap on array A = [5 3 17 10 15 19 6 22 9 28]. To save space and time, you only need to show the content of the tree after each call to heapify(). (That is, slides 24, 25, 27, 29, 32, 36). The following partially filled trees are provided for your convenience. Make sure your final result is indeed a heap.
5
b. (5 points) Exercise # 6.3-2 in textbook page 135: Why do we want the loop index i in algorithm BuildHeap to decrease from blength(A)/ 2 c to 1 rather than increase from 1 to blength(A)/ 2 c?
To call Heapify(A, i), we need to make the assumption that the subtrees rooted at the left and right children of i are both heaps. This assumption is invalid if we start building the heap from A[1].
c. (10 points) Exercise # 6.4-3 in textbook page 136: What is the running time of heapsort on an array A of length n that is already sorted in increasing order? What about decreasing order? (Hint: first think about the cost for building heap in these two cases, then think about the cost for the actual sorting part. You can use examples [0 1 2 3 4 5 6 7 8 9] or [9 8 7 6 5 4 3 2 1 0] to help you think.)
In either case, buildHeap will take Θ(n) time: to build a heap using an array that is sorted in increasing order, we have to sift every elements down (worst-case seneraio), which is in Θ(n); to build a heap using an array that is sorted in decreasing order also takes Θ(n) time because we have to call Heapify n/2 times, even though each call to Heapify will return immediately and takes constant time. After the heap is constructed, each call to heapify takes Θ(log n) time. Therefore the total cost for heap sort is Θ(n log n), regardless of the initial order of the array.
d. (10 points) Starting from the heap below, show the content of the new heap after each heap operation.
20