Introduction to Algorithms

Quicksort
Lecture 3
Prof. Dr. Aydın Öztürk
Quicksort
Divide and Conquer
Partitioning Subroutine
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Example of Partitioning
Running time for PARTITION
The running time of PARTITION
on the subarray A[p...r] is (n)
where n=r-p+1
Pseudo code for Quicksort
Analysis of Quicksort
Worst-case of quicksort
Worst-case decision tree
Worst-case decision tree
Worst-case decision tree
Worst-case decision tree
Worst-case decision tree
Worst-case decision tree
Worst-case analysis
For the worst case, we can write the recurrence equation as
T (n)  max {T (q)  T (n  q  1)}  (n)
0qn 1
We guess that T (n)  cn 2
T (n)  max {cq 2  c(n  q  1) 2 }  (n)
0 q  n 1
 c  max {q 2  (n  q  1) 2 }  (n)
0 q  n 1
Worst-case analysis
This expression achieves a maximum at either end points:
q=0 or q=n-1.
Using the maximum of T(n) we have
T (n)  cn 2  c(2n  1)  (n)
 cn 2
Thus
T (n)  O(n 2 ).
Worst-case analysis
We can also show that the recurrence equation as
T (n)  max {T (q)  T (n  q  1)}  (n)
0qn 1
Has a solution of T (n)  (n 2 )
We guess that
T (n)  cn 2
T (n)  max {cq 2  c(n  q  1) 2 }  (n)
0 qn 1
 c  max {q 2  c(n  q  1) 2 }  (n)
0 qn 1
Worst-case analysis
Using the maximum of T(n) we have
T ( n )  cn 2  c( 2n  1)  ( n )
 cn 2  c( 2n  1)  c1n
 cn 2  ( c1  2c )n  c
 cn 2  ( c1  2c )n
We can pick the constant c1 large enough so that c1  2c  0
and T (n)  cn 2  (n 2 )
Thus the worst case running time of quicksort is (n 2 )
Best-case analysis
Analysis of almost best-case
Analysis of almost best-case
Analysis of almost best-case
Analysis of almost best-case
Analysis of almost best-case
Best-case analysis
For the best case, we can write the recurrence equation as
T (n)  min {T (q)  T (n  q  1)}  (n)
0qn 1
We guess that
T ( n )  cn log n  ( n log n )
Best-case analysis
T ( n)  min {cq log q  c ( n  q  1) log( n  q  1)}  ( n)
0 q  n 1
 c  min {q log q  c ( n  q  1) log( n  q  1)}  ( n)
0 q  n 1
Let Q  q log q  ( n  q  1) log( n  q  1)
( n  q  1)
q
}0
dQ / dq  c{  log q  log( n  q  1) 
( n  q  1)
q
n 1
q
2
Best-case analysis
This expression achieves a minimum at
n 1
q
2
Using the minimum of T(n) we have
n 1
n 1 n 1
n 1
T ( n )  c{
log

log
}  ( n )
2
2
2
2
 cn log( n  1)  c( n  1)  ( n )
 cn log( n  1)  ( n )
 cn log n
 ( n log n )
More intuition
Randomized quicksort
Randomized quicksort
Randomized quicksort
RANDOMIZED-PARTITION (A, p, r)
1
2
3
i←RANDOM(p, r)
exchange A[r]↔A[i]
return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT (A,p,r)
1 if p<r
2 then q←RANDOMIZED-PARTITION (A, p, r)
RANDOMIZED-QUICKSORT (A, p, q-1)
RANDOMIZED-QUICKSORT (A, q+1, r)
Randomized quicksort
Randomized quicksort analysis
Calculating Expectation
Calculating Expectation
Calculating Expectation
Calculating Expectation
Calculating Expectation
Calculating Expectation
Calculating Expectation
n 1
n / 2 1
n 1
k 1
k 1
k  n / 2 
 k lg k   k lg k   k lg k
The lg k in the first summation is bounded above by lg (n/ 2)  lg n  1
The lg k in the second summation is bounded above by lg n
 (lg n  1)
n / 2 1
n 1
k 1
k  n / 2 
 k  lg n  k
n 1
n / 2 1
k 1
k 1
 lg n  k 
k
1
1 n
n
n(n  1) lg n  (  1)
2
2 2
2
1 2
1 2
 n lg n  n
2
8
For n  2 this is the bound.

Substitution Method
Substitution Method
Substitution Method
Substitution Method
Calculating Expectation(Alternative approach)
• Example: Sort the following 10 distinct values:
5, 9, 3, 10, 11, 14, 8, 4, 17, 6
Pivot: 10
{5, 9, 3, 8, 4, 6}, 10, {11, 14, 17}
Pivot: 6
{5, 3, 4}, 6, {9, 8}, 10, {11, 14, 17}
Pivot: 4
{3}, 4, {5}, 6, {9, 8}, 10, {11, 14, 17}
Calculating Expectation(Alternative approach)
• Let X be the total number of comparisons
performed in all calls to PARTITION.
• Let z1 , z2 , , zn with zi
being the ith
smallestelement of the array A.
• We define the set
Z ij   zi , zi 1 ,  , z j 
• Let also
1 if z i is compared to z j 
X ij  

0 otherwise

Calculating Expectation(Alternative
approach)
• To determine the prob. that z i and z j are ever compared, we note that
the values z i , z i 1 , ... z j
will initially in the same bracket and will
remain in the same bracket if the number chosen for the first comparison is
not between z i and z j .
• For example, if the comparison number is larger than z j , then all the
values z i , z i 1 , ... z j will go in a bracket to the left of the comparison
number, and if it is smaller than z i , then they will all go in a bracket to the
right. Thus all the values z i , z i 1 , ... z j will remain in the same
bracket until the first time that one of them chosen as a comparison value.
• At that point all the other values between i and j will be compared with this
comparison value.
• If this comparison value is neither i or j then when compared with it, i will go
into a left bracket and j will go into right bracket, consequently i and j will
never be compared.
• If the comparison value of the set z i , z i 1 , ... z j is either i or j then
there will be a direct comparison between i and j .
Calculating Expectation(Alternative approach)
• The total number number of comparison performed
by the algorithm
X
n 1 n
  X ij
i 1 j  i 1
Calculating Expectation(Alternative approach)
• Taking expectation of both sides
n 1 n

E( X )  E    X ij 
 i 1 j  i 1 

n 1 n
  E[ X ij ]
i 1 j  i 1

n 1 n
  P{zi is compared
i 1 j  i 1
to z j }
Calculating Expectation(Alternative approach)
• Taking expectation of both sides
Pr{zi is compared to z j }  Pr{zi or z j is first pivot chosen from Z ij }
 Pr{zi is first pivot chosen from Z ij }
 Pr{z j is first pivot chosen from Z ij }
1
1


j- i  1 j- i  1
2

j- i  1
Calculating Expectation(Alternative approach)
n-1
E[ X]  
n

i 1 j i 1
2
j  i 1
n-1 n i
2
 
i 1 k 1 k  1
n-1 n
2
 
i 1 k 1 k
n-1
  O(lg n)
i 1
 O(n lg n)
Quicksort in practice