Professional Documents
Culture Documents
Lec4 PDF
Lec4 PDF
6.046J/18.401J
LECTURE 4
Quicksort
Divide and conquer
Partitioning
Worst-case analysis
Intuition
Randomized quicksort
Analysis
Prof. Charles E. Leiserson
September 21, 2005
L4.1
Quicksort
Proposed by C.A.R. Hoare in 1962.
Divide-and-conquer algorithm.
Sorts in place (like insertion sort, but not
like merge sort).
Very practical (with tuning).
L4.2
L4.3
Partitioning subroutine
PARTITION(A, p, q) A[ p . . q]
x A[ p]
pivot = A[ p]
Running
Running time
time
ip
== O(n)
O(n) for
for nn
for j p + 1 to q
elements.
elements.
do if A[ j] x
then i i + 1
exchange A[i] A[ j]
exchange A[ p] A[i]
return i
Invariant: xx
p
September 21, 2005
xx
xx
i
??
j
q
L4.4
Example of partitioning
66 10
10 13
13 55
i
j
88
33
22 11
11
L4.5
Example of partitioning
66 10
10 13
13 55
i
j
88
33
22 11
11
L4.6
Example of partitioning
66 10
10 13
13 55
i
j
88
33
22 11
11
L4.7
Example of partitioning
66 10
10 13
13 55
66
88
33
22 11
11
55 13
13 10
10 88
i
j
33
22 11
11
L4.8
Example of partitioning
66 10
10 13
13 55
66
88
33
22 11
11
55 13
13 10
10 88
i
j
33
22 11
11
L4.9
Example of partitioning
66 10
10 13
13 55
66
88
33
22 11
11
55 13
13 10
10 88
i
33
j
22 11
11
L4.10
Example of partitioning
66 10
10 13
13 55
88
33
22 11
11
66
55 13
13 10
10 88
33
22 11
11
66
55
33 10
10 88 13
13 22 11
11
i
j
L4.11
Example of partitioning
66 10
10 13
13 55
88
33
22 11
11
66
55 13
13 10
10 88
33
22 11
11
66
55
33 10
10 88 13
13 22 11
11
i
j
L4.12
Example of partitioning
66 10
10 13
13 55
88
33
22 11
11
66
55 13
13 10
10 88
33
22 11
11
66
55
33 10
10 88 13
13 22 11
11
66
55
33
22
i
88 13
13 10
10 11
11
j
L4.13
Example of partitioning
66 10
10 13
13 55
88
33
22 11
11
66
55 13
13 10
10 88
33
22 11
11
66
55
33 10
10 88 13
13 22 11
11
66
55
33
22
i
88 13
13 10
10 11
11
j
L4.14
Example of partitioning
66 10
10 13
13 55
88
33
22 11
11
66
55 13
13 10
10 88
33
22 11
11
66
55
33 10
10 88 13
13 22 11
11
66
55
33
22
i
88 13
13 10
10 11
11
L4.15
Example of partitioning
66 10
10 13
13 55
88
33
22 11
11
66
55 13
13 10
10 88
33
22 11
11
66
55
33 10
10 88 13
13 22 11
11
66
55
33
22
88 13
13 10
10 11
11
22
55
33
66
i
88 13
13 10
10 11
11
L4.16
L4.17
Analysis of quicksort
Assume all input elements are distinct.
In practice, there are better partitioning
algorithms for when duplicate input
elements may exist.
Let T(n) = worst-case running time on
an array of n elements.
L4.18
Worst-case of quicksort
Input sorted or reverse sorted.
Partition around min or max element.
One side of partition always has no elements.
(arithmetic series)
L4.19
L4.20
L4.21
L4.22
L4.23
O
(1)
L4.24
n
k = (n 2 )
k =1
O
(1)
L4.25
(1) c(n2)
(1)
n
k = (n 2 )
k =1
O
L4.26
Best-case analysis
(For intuition only!)
1 9
always 10 : 10 ?
L4.27
L4.28
T (101 n )
T (109 n )
L4.29
cn
9
10
cn
9
9
81
1
T (100
n ) T (100
n ) T (100
n )T (100
n)
L4.30
cn
9
100
9
10
cn
O(n)
O(n) leaves
leaves
(1)
cn
log10/9n
9
81
cn
cn
100
100
1
100
cn
cn
cn
cn
cn
(1)
September 21, 2005
L4.31
9
100
9
10
cn
(n lg n)
Lucky!
September 21, 2005
O(n)
O(n) leaves
leaves
(1)
cn
log10/9n
9
81
cn
cn
100
100
log10n
1
cn
100
cn
cn
cn
cn
cn
(1)
cn log10n T(n) cn log10/9n + (n)
Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson
L4.32
More intuition
Suppose we alternate lucky, unlucky,
lucky, unlucky, lucky, .
L(n) = 2U(n/2) + (n) lucky
U(n) = L(n 1) + (n) unlucky
Solving:
L(n) = 2(L(n/2 1) + (n/2)) + (n)
= 2L(n/2 1) + (n)
= (n lg n) Lucky!
How can we make sure we are usually lucky?
September 21, 2005
L4.33
Randomized quicksort
IDEA: Partition around a random element.
Running time is independent of the input
order.
No assumptions need to be made about
the input distribution.
No specific input elicits the worst-case
behavior.
The worst case is determined only by the
output of a random-number generator.
September 21, 2005
L4.34
Randomized quicksort
analysis
Let T(n) = the random variable for the running
time of randomized quicksort on an input of size
n, assuming random numbers are independent.
For k = 0, 1, , n1, define the indicator
random variable
Xk =
L4.35
Analysis (continued)
T(n) =
n 1
= X k (T (k ) + T (n k 1) + (n) )
k =0
L4.36
Calculating expectation
n 1
L4.37
Calculating expectation
n 1
n 1
E[ X k (T (k ) + T (n k 1) + (n) )]
k =0
Linearity of expectation.
L4.38
Calculating expectation
n 1
=
=
n 1
E[ X k (T (k ) + T (n k 1) + (n) )]
k =0
n 1
E[ X k ] E[T (k ) + T (n k 1) + (n)]
k =0
L4.39
Calculating expectation
n 1
=
=
n 1
E[ X k (T (k ) + T (n k 1) + (n) )]
k =0
n 1
E[ X k ] E[T (k ) + T (n k 1) + (n)]
k =0
n 1
n 1
n 1
= 1 E [T (k )] + 1 E [T (n k 1)] + 1 (n)
n k =0
n k =0
n k =0
L4.40
Calculating expectation
n 1
E[T (n)] = E X k (T (k ) + T ( n k 1) + (n) )
k =0
=
=
n 1
E[ X k (T (k ) + T (n k 1) + (n) )]
k =0
n 1
E[ X k ] E[T (k ) + T (n k 1) + (n)]
k =0
n 1
n 1
n 1
= 1 E [T (k )] + 1 E [T (n k 1)] + 1 (n)
n k =0
n k =0
n k =0
n 1
= 2 E [T (k )] + (n)
n k =1
September 21, 2005
Summations have
identical terms.
L4.41
Hairy recurrence
n 1
Use fact:
1 n 2 lg n 1n 2
k
lg
k
(exercise).
2
8
k =2
September 21, 2005
L4.42
Substitution method
n 1
E [T (n)] 2 ak lg k + (n)
n k =2
Substitute inductive hypothesis.
L4.43
Substitution method
n 1
E [T (n)] 2 ak lg k + (n)
n k =2
2a 1 n 2 lg n 1 n 2 + (n)
n 2
8
Use fact.
L4.44
Substitution method
n 1
E [T (n)] 2 ak lg k + (n)
n k =2
2a 1 n 2 lg n 1 n 2 + (n)
n 2
8
= an lg n an (n)
4
L4.45
Substitution method
n 1
E [T (n)] 2 ak lg k + (n)
n k =2
= 2a 1 n 2 lg n 1 n 2 + (n)
n 2
8
= an lg n an (n)
4
an lg n ,
if a is chosen large enough so that
an/4 dominates the (n).
September 21, 2005
L4.46
Quicksort in practice
Quicksort is a great general-purpose
sorting algorithm.
Quicksort is typically over twice as fast
as merge sort.
Quicksort can benefit substantially from
code tuning.
Quicksort behaves well even with
caching and virtual memory.
September 21, 2005
L4.47