Sorting2.6 - Quick Sort Best Time: T(n) = 2T(n/2) + (n) =...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 Worst-case Running Time The worst case for quick-sort 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 worst-case running time of quick-sort 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 Average-Case Running Time If the pivot is selected randomly, the average-case running time for Quick Sort is O(n log n). Proving this requires a probabilistic analysis. We will simply provide an intution for why average-case 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.

Page1 / 5

Sorting2.6 - Quick Sort Best Time: T(n) = 2T(n/2) + (n) =...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online