lec15-quicksort - Lecture Notes CMSC 251 p x

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

View Full Document Right Arrow Icon
Lecture Notes CMSC 251 configuration p r qs x? pr >= x < x q swap x Initial configuration Final configuration Intermediate x < x >= x ? 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+1 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. 47
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture Notes CMSC 251 Final swap 3 7 1 q 8 4 s 5 3 7 3 6 s 4 8 q 1 3 6 3 3 1 3 7 1 8 4 s q 6 3 5 6 q 8 1 s 3 7 6 3 q 8 pr 8 6 4 3 qs 1 7 4 3 q 1 s 4 3 q 1 s 5 4 3 s q 1 Figure 15: Partitioning example. Las Vegas algorithms: These algorithms always produce the correct result, but the running time is a random variable. In these cases the expected running time, averaged over all possible random choices is the measure of the algorithm’s running time. The most well known Monte Carlo algorithm is one for determining whether a number is prime. This is an important problem in cryptography. The QuickSort algorithm that we will discuss today is an ex- ample of a Las Vegas algorithm. Note that QuickSort does not need to be implemented as a randomized algorithm, but as we shall see, this is generally considered the safest implementation. QuickSort Overview: QuickSort is also based on the divide-and-conquer design paradigm. Unlike Merge- Sort where most of the work is done after the recursive call returns, in QuickSort the work is done before the recursive call is made. Here is an overview of QuickSort. Note the similarity with the selec- tion algorithm, which we discussed earlier. Let A [ p..r ] be the (sub)array to be sorted. The initial call is to A [1 ..n ] . Basis: If the list contains 0 or 1 elements, then return.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 351 taught by Professor Staff during the Fall '11 term at University of Louisville.

Page1 / 6

lec15-quicksort - Lecture Notes CMSC 251 p x

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

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