algo_ch7_quick_sort

We end up with three subarrays size 1 n 12 n 12

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: appens if we bad-split root node, then good-split the resulting What badgoodsize (n-1) node? We end up with three subarrays, size 1, (n-1)/2, (n-1)/2 Combined cost of splits = n + n-1 = 2n -1 = Θ(n) No worse than if we had good-split the root node! (n) n n-1 1 (n-1)/2 n Θ(n −1) (n-1)/2 (n-1)/2 Θ( n) (n-1)/2 29 Intuition for the Average Case Suppose, we alternate lucky and unlucky cases to get an Suppose, average behavior L(n) = 2U (n / 2) + Θ(n) lucky U (n) = L(n − 1) + Θ(n) unlucky we consequently get L(n) = 2( L(n / 2 − 1) + Θ(n / 2)) + Θ(n) = 2 L(n / 2 − 1) + Θ (n) = Θ(n log n) The combination of good and bad splits would result in T(n) = Θ(n lg n), but with slightly larger constant hidden by the Θ-notation. 30 Randomized Quicksort An algorithm is randomized if its behavior is An randomized determined not only by the input but also by values produced by a random-number generator. random Exchange A[r] with an element chosen at random Exchange from A[p…r] in Partition. Partition This ensures that the pivot...
View Full Document

Ask a homework question - tutors are online