Download presentation
Presentation is loading. Please wait.
Published byAdelia Harrell Modified over 9 years ago
1
Quicksort Lecture 4
2
Quicksort
3
Divide and Conquer
4
Partitioning Subroutine
5
Example of Partitioning
17
Running time for PARTITION The running time of PARTITION on the subarray A[p...r] is where n=r-p+1
18
Pseudo code for Quicksort
19
Analysis of Quicksort
20
Worst-case of quicksort
21
Worst-case decision tree
27
Worst-case analysis For the worst case, we can write the recurrence equation as We guess that
28
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 Thus
29
Worst-case analysis We can also show that the recurrence equation as Has a solution of We guess that
30
Worst-case analysis Using the maximum of T(n) we have We can pick the constant c 1 large enough so that and Thus the worst case running time of quicksort is
31
Best-case analysis
32
Analysis of almost best-case
37
Best-case analysis For the best case, we can write the recurrence equation as We guess that
38
Best-case analysis
39
This expression achieves a minimum at Using the minimum of T(n) we have
40
More intuition
41
Randomized quicksort
42
Standard Problematic Algorithm :
43
Randomized quicksort R ANDOMIZED-PARTITION (A, p, r) 1i←RANDOM(p, r) 2exchange A[r]↔A[i] 3return PARTITION(A, p, r) RANDOMIZED-QUICKSORT (A,p,r) 1if p<r 2then q← R ANDOMIZED-PARTITION (A, p, r) RANDOMIZED-QUICKSORT (A, p, q-1) RANDOMIZED-QUICKSORT (A, q+1, r)
44
Randomized quicksort
45
Randomized quicksort analysis
46
Calculating Expectation
53
Substitution Method
57
Quicksort in practice
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.