Introduction to Algorithms, Second Edition

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

View Full Document Right Arrow Icon
The University of Texas at Austin Lecture 6 Department of Computer Sciences Professor Vijaya Ramachandran Randomized Quicksort CS357: ALGORITHMS, Spring 2006 Randomized Partition and Randomized Quicksort Randomized-Partition is a simple modification of Partition (which was defined earlier) in which a random element in the subarray being considered is moved to the last position in the subarray (hence this element becomes the pivot), and then Partition is invoked. Rand-Partition ( A, p, r ) i := Random ( p, r ) interchange A [ i ]and A [ r ] return Partition ( A, p, r ) Correctness of Rand-Partition is immediate once we assume correctness of Partition , since A [ i ] is guaranteed to be an element in the subarray A [ p..r ]. The running time remains linear in the size of subarray. We now use Rand-Partition in the randomized version of Quicksort given below. Rand-Quicksort ( A, p, r ) Input. An array A [1 ..n ] of elements from a totally ordered set; p and r are integers with 1 p r n . Output. The elements in sub-array A [ p..r ] are rearranged in sorted order; the elements in array A outside of subarray A [ p..r ] are unchanged. (So initial call is to A [1 ..n ].) if
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 / 3

Lecture 6 - The University of Texas at Austin Department of...

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