Randomized Quicksort Algorithm: UT Austin, CS357: ALGORITHMS, Spring 2007, Study notes of Health sciences

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-gz9
koofers-user-gz9 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The University of Texas at Austin Lecture 6
Department of Computer Sciences
Professor Vijaya Ramachandran Randomized Quicksort
CS357: ALGORITHMS, Spring 2007
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; pand rare integers with
1prn.
Output. The elements in sub-array A[p..r] are rearranged in sorted order; the elements in
array Aoutside 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
Correctness of Rand-Quicksort, assuming correctness of Rand-Partition.
Expected running time of Rand-Quicksort
pf3

Partial preview of the text

Download Randomized Quicksort Algorithm: UT Austin, CS357: ALGORITHMS, Spring 2007 and more Study notes Health sciences in PDF only on Docsity!

The University of Texas at Austin Lecture 6 Department of Computer Sciences Professor Vijaya Ramachandran Randomized Quicksort CS357: ALGORITHMS, Spring 2007

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 p < r then q := Rand-Partition(A, p, r) Rand-Quicksort(A, p, q − 1) Rand-Quicksort(A, q + 1, r) fi

  • Correctness of Rand-Quicksort, assuming correctness of Rand-Partition.
  • Expected running time of Rand-Quicksort

Expected Running Time of Rand-Quicksort

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

  • every comparison between elements in A occurs within some call to Rand-Partition;
  • in every comparison, one of the two elements being compared is the pivot element;
  • in a call to Rand-Partition the pivot element is compared to each of the remaining elements in the subarray exactly once; and
  • after this call to Rand-Partition terminates, the pivot element is in its final position, and is never examined again.

Given the above observation, it follows that

X =

∑^ n

i=

∑^ n

j=i+

Xij

Hence we have

E[X] = E[

n∑− 1

i=

∑^ n

j=i+

Xij ] =

n∑− 1

i=

∑^ n

j=i+

E[Xij ] (by linearity of expectations)

Page 2