Professional Documents
Culture Documents
Use A[r ] as the pivot, and grow partition from left to right.
i will be largest index of processed item ≤ x.
j will be smallest index of unprocessed item.
p i j r
x
x x unrestricted
Use A[r ] as the pivot, and grow partition from left to right.
i will be largest index of processed item ≤ x.
j will be smallest index of unprocessed item.
p i j r
x
x x unrestricted
1 Initially (i, j) = (p − 1, p)
Use A[r ] as the pivot, and grow partition from left to right.
i will be largest index of processed item ≤ x.
j will be smallest index of unprocessed item.
p i j r
x
x x unrestricted
1 Initially (i, j) = (p − 1, p)
2 Increase j by 1 each time to find a place for A[j]
At the same time increase i when necessary
Use A[r ] as the pivot, and grow partition from left to right.
i will be largest index of processed item ≤ x.
j will be smallest index of unprocessed item.
p i j r
x
x x unrestricted
1 Initially (i, j) = (p − 1, p)
2 Increase j by 1 each time to find a place for A[j]
At the same time increase i when necessary
3 Stops when j = r
p i j r
>x x
x x
(A) A[j] > x
p i j r
x
x x
p i j r
<x x
x x
(B) A[j] < x
p i j r
x x
p i j r
>x x
x x
(A) A[j] > x
p i j r
x
x x
p i j r
<x x
x x
(B) A[j] < x
p i j r
x x
1 Only increase j by 1
p i j r
>x x
x x
(A) A[j] > x
p i j r
x
x x
p i j r
<x x
x x
(B) A[j] < x
p i j r
x x
1 Only increase j by 1
2 i = i + 1.
Quicksort Revision of September 5, 2017 6 / 19
One Iteration of the Procedure Partition
Increase j by 1 each time to find a place for A[j]
At the same time increase i when necessary
p i j r
>x x
x x
(A) A[j] > x
p i j r
x
x x
p i j r
<x x
x x
(B) A[j] < x
p i j r
x x
1 Only increase j by 1
2 i = i + 1. A[i] ↔ A[j].
Quicksort Revision of September 5, 2017 6 / 19
One Iteration of the Procedure Partition
Increase j by 1 each time to find a place for A[j]
At the same time increase i when necessary
p i j r
>x x
x x
(A) A[j] > x
p i j r
x
x x
p i j r
<x x
x x
(B) A[j] < x
p i j r
x x
1 Only increase j by 1
2 i = i + 1. A[i] ↔ A[j]. j = j + 1
Quicksort Revision of September 5, 2017 6 / 19
Example: The Operation of Partition(A, p, r )
i p, j r
2 8 7 1 3 5 6 4 (1)
p, i j r
2 8 7 1 3 5 6 4 (2)
p, i j r
2 8 7 1 3 5 6 4 (3)
p, i j r
2 8 7 1 3 5 6 4 (4)
p i j r
2 1 7 8 3 5 6 4 (5)
p i j r
2 1 3 8 7 5 6 4 (6)
p i j r
2 1 3 8 7 5 6 4 (7)
p i j, r
2 1 3 8 7 5 6 4 (8)
p i j, r
2 1 3 4 7 5 6 8 (9)
Quicksort Revision of September 5, 2017 7 / 19
The Partition(A, p, r ) Algorithm
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
if A[j] ≤ x then
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
if A[j] ≤ x then
i = i + 1;
exchange A[i] and A[j];
end
end
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
if A[j] ≤ x then
i = i + 1;
exchange A[i] and A[j];
end
end
; // put pivot in position
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
if A[j] ≤ x then
i = i + 1;
exchange A[i] and A[j];
end
end
exchange A[i + 1] and A[r ]; // put pivot in position
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
if A[j] ≤ x then
i = i + 1;
exchange A[i] and A[j];
end
end
exchange A[i + 1] and A[r ]; // put pivot in position
return
Partition(A, p, r )
begin
x = A[r ]; // A[r ] is the pivot element
i = p − 1;
for j = p to r − 1 do
if A[j] ≤ x then
i = i + 1;
exchange A[i] and A[j];
end
end
exchange A[i + 1] and A[r ]; // put pivot in position
return i + 1 // q = i + 1
end
Running time is O( )
Outline:
Partition
Quicksort
Average Case Analysis of Quicksort
1 Worst-case doesn’t make sense: for any given input, the worst
case is very unlikely to happen
1 Worst-case doesn’t make sense: for any given input, the worst
case is very unlikely to happen
2 Use Average Case Analysis
1 Worst-case doesn’t make sense: for any given input, the worst
case is very unlikely to happen
2 Use Average Case Analysis
3 Assume every possible input permutation of the n items are
equally likely.
1
4 n! permutations, so each one has probability n! of ocurring
5 Let Sn be set of all permutations, and C (σ) denote running
time on input permutation σ ∈ Sn . Then average running
time is
1 X
C (σ)
n!
σ∈Sn
1 X
CN = N −1+ (Ck−1 + CN−k )
N
1≤k≤N
1 X
CN = N −1+ (Ck−1 + CN−k )
N
1≤k≤N
2 X
= N −1+ Ck−1 .
N
1≤k≤N
NCN = (N + 1)CN−1 + 2N − 2
NCN = (N + 1)CN−1 + 2N − 2
CN CN−1 2 2
= + − .
N +1 N N + 1 N(N + 1)
PN
where
P∞ HN = i=1 1/i and we are using the fact that
i=1 1/i(i + 1) is bounded.
Quicksort Revision of September 5, 2017 17 / 19
Average Case Analysis
Hn = ln n + O(1).