

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
An explanation of the randomized quicksort algorithm, which is a modification of the standard quicksort algorithm. The pseudocode for the randomized partition function and the analysis of the expected running time of the randomized quicksort algorithm. The document assumes the reader's familiarity with the quicksort algorithm and some probability theory.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


The University of Texas at Austin Lecture 6 Department of Computer Sciences Professor Vijaya Ramachandran Randomized Quicksort CS357: ALGORITHMS, Spring 2007
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 p < r then q := Rand-Partition(A, p, r) Rand-Quicksort(A, p, q − 1) Rand-Quicksort(A, q + 1, r) fi
Let X be the number of comparisons between pairs of elements in array A made by Rand- Quicksort on an input array A of size n, where we assume that all elements in the input ar- ray are distinct. We will show that E[X] = Θ(n log n). Since the total number of operations executed by Rand-Quicksort is within a constant factor of the number of comparisons performed by it, this will bound the expected running time as Θ(n log n).
We use a ‘slick’ analysis (from the textook) to evaluate E[X]:
For the purpose of analysis, let the elements in array A in increasing order of value be z 1 < z 2 < · · · < zn.
For 1 ≤ i < j ≤ n, let Zij = {zi, · · · , zj }, i.e., Zij is the set of elements in array A with ranks between i and j, both inclusive.
For each of the above pairs of values i, j, we define an indicator random variable Xij for the event that zi is compared to zj , i.e.,
Xij = 1 if zi is compared to zj ; and 0 otherwise
Observation. Rand-Quicksort compares each pair of elements zi, zj at most once.
Why? — This is because
Given the above observation, it follows that
∑^ n
i=
∑^ n
j=i+
Xij
Hence we have
n∑− 1
i=
∑^ n
j=i+
Xij ] =
n∑− 1
i=
∑^ n
j=i+
E[Xij ] (by linearity of expectations)
Page 2