100%(30)30 out of 30 people found this document helpful
This preview shows page 8 - 9 out of 9 pages.
Or, we can prove it correct more directly, as follows. The algorithm above is clearly correct whenn=1.For anyn≥2, suppose the algorithm is correct on all arrays with fewer thannelements (and any indexk).Then on any arrayAof lengthn, we compute an arrayMwithdn/5e<nelements, and so the recursivecall on line 5 correctly finds thedn/10e-th smallest elementpofM. Partitioning putspand its copies intotheir correct positions inA, so if thekth-smallest value ispwe return it correctly on line 8. Otherwise werecurse into the left or right subarray as appropriate, and since these have strictly fewer thannelements therecursive call on line 7 or 9 will return the correct answer. So the algorithm is correct on arrays of sizen,and by induction it is correct in general.Running time:For convenience, assumenis a power of 10; this won’t affect the asymptotic running time.The construction ofMtakesO(n)time, since we computeO(n)medians of a constant number of elementseach. By the definition ofp, there are at leastn/10 elements inMsmaller or equal top. Also, for each suchCS 170, Fall 2014, Sol 38
elementxinM, there are two more elements inAthat are also smaller (the two smallest from the same groupasx), since each element inMis a median of a distinct group of 5 elements (from lines 3–4). Therefore,there are at least 3n/10 elements smaller than or equal topinA. Thereforej≥3n/10, and a symmetricargument shows thati≤7n/10. Thus the recursive calls in lines 7 and 9 are on an array which has at most7n/10 elements. LettingT(n)be the worst-case runtime of the algorithm on an array withnelements (overall possible values ofk), this gives us the recurrenceT(n)≤T(n/5)+T(7n/10)+O(n).In particular,T(n)≤T(n/5)+T(7n/10)+cn.for some constantc.We’ll use guess-and-check to solve this recurrence; in particular, we will show that the solution to thisrecurrence satisfiesT(n) =O(n). Take anyd≥10csuch thatd≥T(1). We prove by strong induction thatT(n)≤dnfor alln. The base case holds since we requiredT(1)≤d. For anyn>1, suppose thatT(k)≤dkfor allk<n. Then we haveT(n)≤T(n/5)+T(7n/10)+cn≤dn/5+7dn/10+cn≤9dn/10+dn/10=dn.So by inductionT(n)≤dnfor alln, and thusT(n) =O(n).Comments:Why groups of 5?Why not divide the array into groups of 3?Because then we’d get therecurrenceT(n)≤T(n/3)+T(2n/3)+O(n), which solves toO(nlogn): no good.Why not divide the array into groups of 4? Because odd numbers are convenient (what’s the median of agroup of 4 elements?).Why not divide the array into groups of 7? That would work. 5 elements just happens to be the smallest oddnumber where the recurrence solves toO(n).Incidentally, notice how much simpler the randomized version of this algorithm was?The randomizedversion also performs better in practice. This example illustrates how randomization can sometimes makealgorithms simpler and more efficient—a recurring theme in algorithms.
You've reached the end of your free preview.
Want to read all 9 pages?
Fall '02
HENZINGER
Algorithms, Big O notation, Analysis of algorithms, Selection algorithm