11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 15.7 Optimal Sorting 501While selecting the median in this way is guaranteed to elimin<strong>at</strong>e a fraction ofthe elements (leaving <strong>at</strong> most ⌈(7n − 5)/10⌉ elements left), we still need to be sureth<strong>at</strong> our recursion yields a linear-time algorithm. We model the algorithm by thefollowing recurrence.T(n) ≤ T(⌈n/5⌉) + T(⌈(7n − 5)/10⌉) + 6⌈n/5⌉ + n − 1.The T(⌈n/5⌉) term comes from computing the median of the medians-of-fives,the 6⌈n/5⌉ term comes from the cost to calcul<strong>at</strong>e the median-of-fives (exactly sixcomparisons for each group of five element), <strong>and</strong> the T(⌈(7n−5)/10⌉) term comesfrom the recursive call of the remaining (up to) 70% of the elements th<strong>at</strong> might beleft.We will prove th<strong>at</strong> this recurrence is linear by assuming th<strong>at</strong> it is true for someconstant r, <strong>and</strong> then show th<strong>at</strong> T(n) ≤ rn for all n gre<strong>at</strong>er than some bound.T(n) ≤ T(⌈ n − 5⌉) + T(⌈7n ⌉) + 6⌈ n 5 10 5 ⌉ + n − 1≤ r( n − 5+ 1) + r(7n + 1) + 6( n 5 105 + 1) + n − 1≤ ( r 5 + 7r10 + 11 3r)n +5 2 + 5≤9r + 22n +103r + 10.2This is true for r ≥ 23 <strong>and</strong> n ≥ 380. This provides a base case th<strong>at</strong> allows us touse induction to prove th<strong>at</strong> ∀n ≥ 380, T(n) ≤ 23n.In reality, this algorithm is not practical because its constant factor costs are sohigh. So much work is being done to guarantee linear time performance th<strong>at</strong> it ismore efficient on average to rely on chance to select the pivot, perhaps by pickingit <strong>at</strong> r<strong>and</strong>om or picking the middle value out of the current subarray.15.7 Optimal SortingWe conclude this section with an effort to find the sorting algorithm with the absolutefewest possible comparisons. It might well be th<strong>at</strong> the result will not bepractical for a general-purpose sorting algorithm. But recall our analogy earlier tosports tournaments. In sports, a “comparison” between two teams or individualsmeans doing a competition between the two. This is fairly expensive (<strong>at</strong> least comparedto some minor book keeping in a computer), <strong>and</strong> it might be worth trading afair amount of book keeping to cut down on the number of games th<strong>at</strong> need to beplayed. Wh<strong>at</strong> if we want to figure out how to hold a tournament th<strong>at</strong> will give usthe exact ordering for all teams in the fewest number of total games? Of course,we are assuming th<strong>at</strong> the results of each game will be “accur<strong>at</strong>e” in th<strong>at</strong> we assume

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!