Docsity

Lecture 14: HeapSort Analysis and Partitioning, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
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−1calls to Heapify,
each of which takes O(log n)time. So the total running time is O((n−1) log n)=O(nlogn).
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(nlog n)time to build a heap, because we need
to apply Heapify roughly n/2times (to each of the internal nodes), and (2) that it takes O(nlog n)
time to extract each of the maximum elements, since we need to extract roughly nelements and each
extraction involves a constant amount of work and one Heapify. Therefore the total running time of
HeapSort is O(nlog n).
Is this tight? That is, is the running time Θ(nlog n)? The answer is yes. In fact, later we will see that it
is not possible to sort faster than Ω(nlog 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 Θ(nlog 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 nis of the form n=2
h+1 −1, where his 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
will save us from worrying about floors and ceilings.
With this assumption, level 0 of the tree has 1 node, level 1 has 2 nodes, and up to level h, which has
2hnodes. All the leaves reside on level h.
Recall that when Heapify is called, the running time depends on how far an element might sift down
before the process terminates. In the worst case the element might sift down all the way to the leaf
level. Let us count the work done level by level.
At the bottommost level there are 2hnodes, but we do not call Heapify on any of these so the work is
0. At the next to bottommost level there are 2h−1nodes, and each might sift down 1 level. At the 3rd
level from the bottom there are 2h−2nodes, and each might sift down 2 levels. In general, at level j
44
pf3
pf4

Partial preview of the text

Download Lecture 14: HeapSort Analysis and Partitioning and more Slides Data Structures and Algorithms in PDF only on Docsity!

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 = 2h+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 will save us from worrying about floors and ceilings. With this assumption, level 0 of the tree has 1 node, level 1 has 2 nodes, and up to level h, which has 2 h^ nodes. All the leaves reside on level h. Recall that when Heapify is called, the running time depends on how far an element might sift down before the process terminates. In the worst case the element might sift down all the way to the leaf level. Let us count the work done level by level. At the bottommost level there are 2 h^ nodes, but we do not call Heapify on any of these so the work is

  1. At the next to bottommost level there are 2 h−^1 nodes, and each might sift down 1 level. At the 3rd level from the bottom there are 2 h−^2 nodes, and each might sift down 2 levels. In general, at level j

0 0 0 0 0 0

2*

0 0*

1*

3*

Total work for BuildHeap

2

0

1 1 1 1

2

3

Figure 13: Analysis of BuildHeap.

from the bottom there are 2 h−j^ nodes, and each might sift down j levels. So, if we count from bottom to top, level-by-level, we see that the total time is proportional to

T (n) =

∑^ h

j=

j 2 h−j^ =

∑^ h

j=

j 2 h 2 j^

If we factor out the 2 h^ term, we have

T (n) = 2h

∑h

j=

j 2 j^

This is a sum that we have never seen before. We could try to approximate it by an integral, which would involve integration by parts, but it turns out that there is a very cute solution to this particular sum. We’ll digress for a moment to work it out. First, write down the infinite general geometric series, for any constant x < 1. ∑∞

j=

xj^ =

1 − x

Then take the derivative of both sides with respect to x, and multiply by x giving:

∑^ ∞

j=

jxj−^1 =

(1 − x)^2

∑^ ∞

j=

jxj^ = x (1 − x)^2

and if we plug x = 1/ 2 , then voila! we have the desired formula:

∑^ ∞

j=

j 2 j^

(1 − (1/2))^2

In our case we have a bounded sum, but since the infinite series is bounded, we can use it instead as an easy approximation.

Using this we have

T (n) = 2h

∑h

j=

j 2 j^

≤ 2 h

∑^ ∞

j=

j 2 j^

≤ 2 h^ · 2 = 2h+1.

Now recall that n = 2h+1^ − 1 , so we have T (n) ≤ n + 1 ∈ O(n). Clearly the algorithm takes at least Ω(n) time (since it must access every element of the array at least once) so the total running time for BuildHeap is Θ(n).

configuration

p (^) r

q s

x?

p r

q s

< x >= x

q

swap

x

Initial configuration

Final configuration

x < x >= x? Intermediate

Figure 14: Partitioning intermediate structure.

all of the elements have been processed. To finish things off we swap A[p] (the pivot) with A[q], and return the value of q. Here is the complete code: Partition

Partition(int p, int r, array A) { // 3-way partition of A[p..r] x = A[p] // pivot item in A[p] q = p for s = p+1 to r do { if (A[s] < x) { q = q+ swap A[q] with A[s] } } swap A[p] with A[q] // put the pivot into final position return q // return location of pivot }

An example is shown below.

Lecture 15: QuickSort

(Tuesday, Mar 17, 1998)

Revised: March 18. Fixed a bug in the analysis.

Read: Chapt 8 in CLR. My presentation and analysis are somewhat different than the text’s.

QuickSort and Randomized Algorithms: Early in the semester we discussed the fact that we usually study the worst-case running times of algorithms, but sometimes average-case is a more meaningful measure. Today we will study QuickSort. It is a worst-case Θ(n^2 ) algorithm, whose expected-case running time is Θ(n log n). We will present QuickSort as a randomized algorithm, that is, an algorithm which makes random choices. There are two common types of randomized algorithms:

Monte Carlo algorithms: These algorithms may produce the wrong result, but the probability of this occurring can be made arbitrarily small by the user. Usually the lower you make this probability, the longer the algorithm takes to run.