This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Quick Sort
Best Time: T(n) = 2T(n/2) + (n)
= (n log n) Worst Time:
Expected Time: CSE 2011
Prof. J. Elder  81  Last Updated: 4/1/10 11:16 AM Worstcase Running Time
The worst case for quicksort occurs when the pivot is the unique
minimum or maximum element
One of L and G has size n 1 and the other has size 0
The running time is proportional to the sum
n + (n 1) + … + 2 + 1 Thus, the worstcase running time of quicksort is O(n2)
depth time
0 n 1 n …
n
CSE 2011
Prof. J. Elder 1 …
1 1
 82  Last Updated: 4/1/10 11:16 AM AverageCase Running Time
If the pivot is selected randomly, the averagecase running time
for Quick Sort is O(n log n).
Proving this requires a probabilistic analysis.
We will simply provide an intution for why averagecase O(n log n)
is reasonable.
depth
0
1
…
h
CSE 2011
Prof. J. Elder  83  Last Updated: 4/1/10 11:16 AM Expected Time Complexity for Quick Sort
Q: Why is it reasonable to expect O (n log n) time complexity? CSE 2011
Prof. J. Elder  84  Last Updated: 4/1/10 11:16 AM Expected Time Complexity for Quick Sort
Then T (n) = T ( p (n 1)) + T (q (n 1)) + O (n) wlog, suppose that q > p .
Let k be the depth of the recursion tree
Then q k n = 1 k = log n / log(1 / q )
Thus k O (log n) : O (n) work done per level CSE 2011
Prof. J. Elder T (n) = O (n log n).  85  Last Updated: 4/1/10 11:16 AM ...
View
Full
Document
This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.
 Fall '11
 Elder
 Data Structures, Sort, Quick Sort

Click to edit the document details