Randomized quicksort

advertisement
Quicksort
Lecture 4
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 {cq2  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)  max {cq2  c(n  q  1)2 }  (n)
0 qn 1
T (n)  cn2  c(2n  1)  (n)
 cn2
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 {cq2  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
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)  cn2  (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  ( n  q  1) log(n  q  1)}  ( n )
0 q  n 1
Let Q  q log q  ( n  q  1) log(n  q  1)
q
( n  q  1)
dQ / dq  c{  log q  log(n  q  1) 
} 0
q
( n  q  1)
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
Standard Problematic Algorithm :
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
T helg k in thefirst summationis bounded above by lg (n/ 2)  lg n  1
T helg k in thesecondsummationis 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
1
 n 2 lg n  n 2
2
8
For n  2 thisis thebound.
Substitution Method
Substitution Method
Substitution Method
Substitution Method
Quicksort in practice
Download