



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
Heapsort Algorithm, Heapsort, Heapify Procedure, Example of heap sort, Analysis of Heapify, Build Heap, Analysis of BuildHeap, Analysis of Heapsort are the key points in this study notes file.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




We build a max heap out of the given array of numbers A[1..n]. We repeatedly extract the the maximum item from the heap. Once the max item is removed, we are left with a hole at the root. To fix this, we will replace it with the last leaf in tree. But now the heap order will very likely be destroyed. We will apply a heapify procedure to the root to restore the heap. Figure 4.2 shows an array being sorted.
1 BUILD- HEAP(A, n) 2 m ← n 3 while (m ≥ 2) 4 do SWAP(A[1],A[m]) 5 m ← m − 1 6 HEAPIFY(A, 1,m) 87 57 44 12 15 19 23 1 2 3 4 5 6 7 0 87 57 44 12 15 19 23
There is one principal operation for maintaining the heap property. It is called Heapify. (In other books it is sometimes called sifting down.) The idea is that we are given an element of the heap which we suspect may not be in valid heap order, but we assume that all of other the elements in the subtree rooted at this element are in heap order. In particular this root element may be too small. To fix this we “sift” it down the tree by swapping it with one of its children. Which child? We should take the larger of the two children to satisfy the heap ordering property. This continues recursively until the element is either larger than both its children or until its falls all the way to the leaf level. Here is the algorithm. It is given the heap in the array A, and the index i of the suspected element, and m the current active size of the heap. The element A[max] is set to the maximum of A[i] and it two children. If max 6= i then we swap A[i] and A[max] and then recurse on A[max].
x 20 x 8 Figure 4.3: Total work performed in buildheap
At the bottom most level, there are 2 h nodes but we do not heapify these. At the next level up, there are 2 h-1 nodes and each might shift down 1. In general, at level j, there are 2 h-j nodes and each may shift down j levels.
So, if count from bottom to top, level-by-level, the total time is
We can factor out the 2 h^ term:
How do we solve this sum? Recall the geometric series, for any constant x < 1
Take the derivative with respect to x and multiply by x
We plug x = 1/2 and we have the desired formula:
In our case, we have a bounded sum, but since we the infinite series is bounded, we can use it as an easy approximation:
Recall that n = 2 h+
T(n) ≤ n + 1 ∈ O(n)
The algorithm takes at least (n) time since it must access every element at once. So the total time for BuildHeap is Θ(n).
BuildHeap is a relatively complex algorithm. Yet, the analysis yield that it takes _(n) time. An intuitive way to describe why it is so is to observe an important fact about binary trees The fact is that the vast majority of the nodes are at the lowest level of the tree. For example, in a complete binary tree of height h, there is a total of n Θ 2 h+1 nodes. The number of nodes at the bottom three levels alone is
Almost 90% of the nodes of a complete binary tree reside in the 3 lowest levels. Thus, algorithms that operate on trees should be efficient (as BuildHeap is) on the bottom-most levels since that is where most of the weight of the tree resides.
Heapsort calls BuildHeap once. This takes _(n). Heapsort then extracts roughly n maximum elements from the heap. Each extract requires a constant amount of work (swap) and O(log n) heapify. Heapsort is thus O(n log n).
Is HeapSort Θ(n log n)? The answer is yes. In fact, later we will show that comparison based sorting algorithms can not run faster than Ω(n log n). Heapsort is such an algorithm and so is Mergesort that we saw ealier.