lect14-heapsort-analysis-part - Lecture Notes CMSC 251...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture Notes CMSC 251 Heapify(A, 1, m) // fix things up } } An example of HeapSort is shown in Figure 7.4 on page 148 of CLR. We make n - 1 calls to Heapify, each of which takes O (log n ) time. So the total running time is O (( n - 1) log n )= O ( n log n ) . Lecture 14: HeapSort Analysis and Partitioning (Thursday, Mar 12, 1998) Read: Chapt 7 and 8 in CLR. The algorithm we present for partitioning is different from the texts. HeapSort Analysis: Last time we presented HeapSort. Recall that the algorithm operated by first building a heap in a bottom-up manner, and then repeatedly extracting the maximum element from the heap and moving it to the end of the array. One clever aspect of the data structure is that it resides inside the array to be sorted. We argued that the basic heap operation of Heapify runs in O (log n ) time, because the heap has O (log n ) levels, and the element being sifted moves down one level of the tree after a constant amount of work. Based on this we can see that (1) that it takes O ( n log n ) time to build a heap, because we need to apply Heapify roughly n/ 2 times (to each of the internal nodes), and (2) that it takes O ( n log n ) time to extract each of the maximum elements, since we need to extract roughly n elements and each extraction involves a constant amount of work and one Heapify. Therefore the total running time of HeapSort is O ( n log n ) . Is this tight? That is, is the running time Θ( n log n ) ? The answer is yes. In fact, later we will see that it is not possible to sort faster than Ω( n log n ) time, assuming that you use comparisons, which HeapSort does. However, it turns out that the first part of the analysis is not tight. In particular, the BuildHeap procedure that we presented actually runs in Θ( n ) time. Although in the wider context of the HeapSort algorithm this is not significant (because the running time is dominated by the Θ( n log n ) extraction phase). Nonetheless there are situations where you might not need to sort all of the elements. For example, it is common to extract some unknown number of the smallest elements until some criterion (depending on the particular application) is met. For this reason it is nice to be able to build the heap quickly since you may not need to extract all the elements. BuildHeap Analysis: Let us consider the running time of BuildHeap more carefully. As usual, it will make our lives simple by making some assumptions about n . In this case the most convenient assumption is that n is of the form n =2 h +1 - 1 , where h is the height of the tree. The reason is that a left-complete tree with this number of nodes is a complete tree, that is, its bottommost level is full. This assumption
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

lect14-heapsort-analysis-part - Lecture Notes CMSC 251...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online