Tutorial 5 Q 3
Tutorial 5 Q 3
Question 1: In class we learned the Randomized Selection algorithm and used a geometric series anal-
ysis approach to show that it runs in O(n) expected time.
In this tutorial you will re-derive the O(n) time in a different way, using the Indicator
Random Variable method used to analyze Quicksort.
Recall the Randomized Selection algorithm to find the k-th smallest element
Pick a random pivot, divide the array into 3 parts:
We then either stop immediately or recursively solve the problem in the left OR the right
part of the array.
I) Define:
1 if zi and zj are compared by the algorithm
Xij =
0 otherwise
Prove the following three facts
2
(a) i ≤ k ≤ j : Pr[Xij = 1] = j−i+1
2
(b) i < j < k : Pr[Xij = 1] = k−i+1
2
(c) k < i < j : Pr[Xij = 1] = j−k+1
II) By the indicator random variable technique, the expected total number of compar-
isons is
E[Xij ] = Pr[Xij = 1]
j<j i<j
Use this to thow that i<j E[Xij ] = O(n).
Question 2: The analysis of the expected running time of randomized quicksort in the lecture notes
assumed that all element values are distinct. In this problem, we examine what happens
when they are not.
(a) Suppose that all element values are equal. What would be randomized quicksort’s
running time?
(b) The PARTITION procedure taught returns an index q such that each element of
A[p, · · · , q − 1] is less than or equal to A[q] and each element of A[q + 1, · · · , r] is
greater than A[q].
Modify PARTITION to produce a new procedure PARTITION′ (A, p, r), which per-
mutes the elements of A[p, · · · , r] and returns two indices q, t, where p ≤ q ≤ t ≤ r,
such that:
∗ all elements of A[q, · · · , t] are equal,
∗ each element of A[p, · · · , q − 1] is less than A[q], and
∗ each element of A[t + 1, · · · , r] is greater than A[q]
1
Like PARTITION, your PARTITION′ procedure should take Θ(r − p) time.
(c) Modify the QUICKSORT procedure to produce QUICKSORT′ (A, p, r) that calls
PARTITION′ but then only recurses on A[p, q − 1] and A[t + 1, r]
(d) [Advanced Topic: Not needed for class] Our analysis of QUICKSORT in class as-
sumed that all elements were distinct. Using QUICKSORT′ , how would you adjust
the analysis (binary tree plus indicator random variables) in the lecture notes to
avoid the assumption that all elements are distinct?