Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LECTURE 4. Quicksort
SEN-920 Computer Algorithms
Agenda
Quicksort
Divide and conquer
Partitioning
Worst-case analysis
Intuition
Randomized quicksort
Analysis
Quicksort
Partitioning Subroutine
PARTITION(A, p, q) A[ p . . q]
x A[ p] pivot = A[ p] Running time
ip = O(n) for n
for j p + 1 to q elements.
do if A[ j] x
then i i + 1
exchange A[i] A[ j]
exchange A[ p] A[i]
return i
Invariant: xx xx xx ??
p i j q
Based on slides by Erik Demaine and Charles Leiserson 2/7/2017
SEN-920 Computer Algorithms
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 10 8 13 2 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 10 8 13 2 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
6 5 3 2 8 13 10 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
6 5 3 2 8 13 10 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
6 5 3 2 8 13 10 11
i j
Example of Partitioning
66 10
10 13
13 55 88 33 22 11
11
6 5 13 10 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
6 5 3 2 8 13 10 11
2 5 3 6 8 13 10 11
2 5 3 6 8 13 10 11
i
QUICKSORT(A, p, r)
if p < r
then q PARTITION(A, p, r)
QUICKSORT(A, p, q1)
QUICKSORT(A, q+1, r)
Analysis of Quicksort
Worst-Case of Quicksort
k 1
T(0) c(n1)
T(0) c(n2)
T(0)
(1)
(1) c(n1)
k 1
(1) c(n2)
h=n T(n) = (n) + (n2)
(1) = (n2)
(1)
Best-Case Analysis
T (n)
cn
T T
1 9
10 10
cn
1 9
10
cn 10
cn
cn cn
1
10
cn 9
10
cn cn
log10n log 10/9 n
1 9 9
100 cn 100 cn 100 cn 81
100 cn
cn
(1) O(n) leaves
(n lg n) (1)
Lucky! cn log10n T(n) cn log10/9n + O(n)
Based on slides by Erik Demaine and Charles Leiserson 2/7/2017
SEN-920 Computer Algorithms
More Intuition
Randomized Quicksort
IDEA: Partition around a random element.
RANDOMIZED-PARTITION(A, p, r)
i = RANDOM(p, r)
Exchange A[p] with A[i]
return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, r)
if p < r
then q RANDOMIZED-PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, q1)
RANDOMIZED-QUICKSORT(A, q+1, r)
Based on slides by Erik Demaine and Charles Leiserson 2/7/2017
SEN-920 Computer Algorithms
Analysis (continued)
Calculating Expectation
E[T(n)] =
n1
E X k T (k) T (n k 1) (n)
k 0
Calculating Expectation
Linearity of expectation.
E[T(n)]
= E + 1 +
=0
1
= [ + 1 + ]
=0
Calculating Expectation
E[T(n)]
E + 1 +
=0
n1
EX k T(k) T (n k 1) (n)
k 0
n1
EX k ET (k) T (n k 1) (n)
k 0
Calculating Expectation
E[T(n)]
E + 1 +
=0
n1
EX k T(k) T (n k 1) (n)
k 0
n1
EX k ET (k) T (n k 1) (n)
k 0
n1 n1 n 1
1n ET (k) 1n ET (n k 1) 1n (n)
k 0 k 0 k 0
Calculating Expectation
E[T(n)]
E + 1 +
=0
n1
EX k T(k) T (n k 1) (n)
k 0
n1
EX k ET (k) T (n k 1) (n)
k 0
n1 n1 n 1
1n ET (k) 1n ET (n k 1) 1n (n)
k 0 k 0 k 0
n1
2n ET (k) (n) Summations have
k 0 identical terms.
Based on slides by Erik Demaine and Charles Leiserson 2/7/2017
SEN-920 Computer Algorithms
Hairy Recurrence
n1
E[T (n)] 2 ET(k ) (n)
n k =0
(The k = 0 term can be absorbed in the (n).)
Prove by induction: E[T(n)] an lg n for
constant a > 0 .
Choose a large enough so that an lg n
dominates E[T(n)] for sufficiently large n 2.
n1
Use fact: k lg k 1 n 2 lg n 1n 2
2 8
k1
Based on slides by Erik Demaine and Charles Leiserson 2/7/2017
SEN-920 Computer Algorithms
Upper Bound
Substitution Method
n1
ET (n) 2 ak lg k (n)
n k1
Substitute inductive hypothesis.
Substitution Method
n1
ET (n) 2 ak lg k (n)
n k1
2 1 2 1 2
( n lg n n ) (n)
2 8
Substitution Method
n1
ET (n) 2 ak lg k (n)
n k1
2 1 2 1 2
( n lg n n ) (n)
2 8
anlgn ( 4 (n))
Express as desired residual.
Substitution Method
n1
ET (n) 2 ak lg k (n)
n k1
2 1 2 1 2
( n lg n n ) (n)
2 8
anlgn ( 4 (n))
anlgn,
if a is chosen large enough so that
4
dominates the (n).
Quicksort in Practice