1. 1
Randomizing Quicksort
• Randomly permute the elements of the input
array before sorting
• OR ... modify the PARTITION procedure
– At each step of the algorithm we exchange element
A[p] with an element chosen at random from A[p…r]
– The pivot element x = A[p] is equally likely to be any
one of the r – p + 1 elements of the subarray
2. 2
Randomized Algorithms
• No input can elicit worst case behavior
– Worst case occurs only if we get “unlucky” numbers
from the random number generator
• Worst case becomes less likely
– Randomization can NOT eliminate the worst-case but
it can make it less likely!
4. 4
Randomized Quicksort
Alg. : RANDOMIZED-QUICKSORT(A, p, r)
if p < r
then q ← RANDOMIZED-PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, q)
RANDOMIZED-QUICKSORT(A, q + 1, r)
5. 5
Notation
• Rename the elements of A as z1, z2, . . . , zn, with
zi being the i-th smallest element
• Define the set Zij = {zi , zi+1, . . . , zj } the set of
elements between zi and zj, inclusive
106145389 72
z1z2 z9 z8 z5z3 z4 z6 z10 z7
6. 6
Total Number of Comparisons
in PARTITION
i+1 n
=X
i n-1
∑
−
=
1
1
n
i
∑+=
n
ij 1 ijX
• Total number of comparisons X performed by
the algorithm:
• Define Xij = I {zi is compared to zj }
7. 7
Expected Number of Total
Comparisons in PARTITION
• Compute the expected value of X:
=][XE
by linearity
of expectation
the expectation of Xij is equal
to the probability of the event
“zi is compared to zj”
=
∑ ∑
−
= +=
1
1 1
n
i
n
ij
ijXE [ ]=∑ ∑
−
= +=
1
1 1
n
i
n
ij
ijXE
∑ ∑
−
= +=
=
1
1 1
}Pr{
n
i
n
ij
ji ztocomparedisz
indicator
random variable
8. 8
Comparisons in PARTITION :
Observation 1
• Each pair of elements is compared at most once
during the entire execution of the algorithm
– Elements are compared only to the pivot point!
– Pivot point is excluded from future calls to PARTITION
9. 9
Comparisons in PARTITION:
Observation 2
• Only the pivot is compared with elements in both
partitions!
z1z2 z9 z8 z5z3 z4 z6 z10 z7
106145389 72
Z1,6= {1, 2, 3, 4, 5, 6} Z8,9 = {8, 9, 10}{7}
pivot
Elements between different partitions
are never compared!
10. 10
Comparisons in PARTITION
• Case 1: pivot chosen such as: zi < x < zj
– zi and zj will never be compared
• Case 2: zi or zj is the pivot
– zi and zj will be compared
– only if one of them is chosen as pivot before any
other element in range zi to zj
106145389 72
z1z2 z9 z8 z5z3 z4 z6 z10 z7
Z1,6= {1, 2, 3, 4, 5, 6} Z8,9 = {8, 9, 10}{7}
Pr{ }?i jz is compared to z
11. 11
Probability of comparing zi with zj
= 1/( j - i + 1) + 1/( j - i + 1) = 2/( j - i + 1)
zi is compared to zj
zi is the first pivot chosen from Zij
=Pr{ }
Pr{ }
zj is the first pivot chosen from Zij
Pr{ }
OR+
•There are j – i + 1 elements between zi and zj
– Pivot is chosen randomly and independently
– The probability that any particular element is the first
one chosen is 1/( j - i + 1)
12. 12
Number of Comparisons in PARTITION
1 1 1 1
1 1 1 1 1 1 1
2 2 2
[ ] (lg )
1 1
n n n n i n n n
i j i i k i k i
E X O n
j i k k
− − − − −
= = + = = = = =
= = < =
− + +
∑ ∑ ∑∑ ∑∑ ∑
)lg( nnO=
⇒ Expected running time of Quicksort using
RANDOMIZED-PARTITION is O(nlgn)
∑ ∑
−
= +=
=
1
1 1
}Pr{][
n
i
n
ij
ji ztocomparediszXE
Expected number of comparisons in PARTITION:
(set k=j-i) (harmonic series)
13. 13
Alternative Average-Case
Analysis of Quicksort
• See Problem 7-2, page 16
• Focus on the expected running time of
each individual recursive call to Quicksort,
rather than on the number of comparisons
performed.
• Use Hoare partition in our analysis.
14. 14
Worst-Case Analysis
T(n) = max (T(q) + T(n - q - 1)) + Θ(n)
0 ≤ q ≤ n-1
1.the time to sort the left partition with q elements, plus
2.the time to sort the right partition with N-q-1 elements, plus
3.the time to build the partitions
where q ranges from 0 to n-1 since the procedure PARTITION
produces two sub-problems with total size n-1
---------- Substitution method: Guess T(n) ≤ cn2
T(n) ≤ max (cq2
+ c(n - q - 1)2
) + Θ(n)
0 ≤ q ≤ n-1
= c · max (q2
+ (n - q - 1)2
) + Θ(n)
0 ≤ q ≤ n-1
Take derivatives to get maximum at q = 0, n-1:
T(n) ≤ c(n - 1)2
+ Θ(n) ≤ cn2
- 2c(n - 1) + 1 + Θ(n) ≤ cn2
Therefore, the worst case running time is Θ(n2
)