algo_ch7_quick_sort

N cn2 therefore the worst case running time is n2 23

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: c(n - 1)2 + Θ(n) ≤ cn2 - 2c(n - 1) + 1 + Θ(n) ≤ cn2 Therefore, the worst case running time is Θ(n2) 23 Best Case Partition When the partitioning procedure produces two When regions of size n/2, we get the a balanced partition balanced with best case performance: best T(n) = 2T(n/2) + Θ(n) = Θ(n lg n) Average complexity is also Θ(n lg n) Average 24 Best Case Partitioning 25 Average Case Assuming random input, average-case running time Assuming is much closer to Θ(n lg n) than Θ(n2) First, a more intuitive explanation/example: First, Suppose that partition() always produces a 9-to-1 proportional split. This looks quite unbalanced! The recurrence is thus: T(n) = T(9n/10) + T(n/10) + Θ(n) = Θ(n lg n)! How deep will the recursion go? 26 Average Case T (n) = T (n /10) + T (9n /10) + Θ( n) = Θ( n log n)! For a split of proportionality α, where 0≤α≤½, the minimum depth of the tree is - lg n / lg α & the maximum depth is - lg n / lg(1-α). log2n = log10n/log102 27 Average Case Intuitively, a real-life run of quicksort will produce a Intuitively, mix of “bad” and “good” splits Randomly distributed among the recursion tree Pretend for intuition that they alternate between best-case (n/2:n/2) and worst-case (n-1:1) 28 Average Case What h...
View Full Document

Ask a homework question - tutors are online