Or we can prove it correct more directly as follows The algorithm above is

Or we can prove it correct more directly as follows

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 when n = 1. For any n 2, suppose the algorithm is correct on all arrays with fewer than n elements (and any index k ). Then on any array A of length n , we compute an array M with d n / 5 e < n elements, and so the recursive call on line 5 correctly finds the d n / 10 e -th smallest element p of M . Partitioning puts p and its copies into their correct positions in A , so if the k th-smallest value is p we return it correctly on line 8. Otherwise we recurse into the left or right subarray as appropriate, and since these have strictly fewer than n elements the recursive call on line 7 or 9 will return the correct answer. So the algorithm is correct on arrays of size n , and by induction it is correct in general. Running time: For convenience, assume n is a power of 10; this won’t affect the asymptotic running time. The construction of M takes O ( n ) time, since we compute O ( n ) medians of a constant number of elements each. By the definition of p , there are at least n / 10 elements in M smaller or equal to p . Also, for each such CS 170, Fall 2014, Sol 3 8
Image of page 8
element x in M , there are two more elements in A that are also smaller (the two smallest from the same group as x ), since each element in M is a median of a distinct group of 5 elements (from lines 3–4). Therefore, there are at least 3 n / 10 elements smaller than or equal to p in A . Therefore j 3 n / 10, and a symmetric argument shows that i 7 n / 10. Thus the recursive calls in lines 7 and 9 are on an array which has at most 7 n / 10 elements. Letting T ( n ) be the worst-case runtime of the algorithm on an array with n elements (over all possible values of k ), this gives us the recurrence T ( n ) T ( n / 5 )+ T ( 7 n / 10 )+ O ( n ) . In particular, T ( n ) T ( n / 5 )+ T ( 7 n / 10 )+ cn . for some constant c . We’ll use guess-and-check to solve this recurrence; in particular, we will show that the solution to this recurrence satisfies T ( n ) = O ( n ) . Take any d 10 c such that d T ( 1 ) . We prove by strong induction that T ( n ) dn for all n . The base case holds since we required T ( 1 ) d . For any n > 1, suppose that T ( k ) dk for all k < n . Then we have T ( n ) T ( n / 5 )+ T ( 7 n / 10 )+ cn dn / 5 + 7 dn / 10 + cn 9 dn / 10 + dn / 10 = dn . So by induction T ( n ) dn for all n , and thus T ( n ) = O ( n ) . Comments: Why groups of 5? Why not divide the array into groups of 3? Because then we’d get the recurrence T ( n ) T ( n / 3 )+ T ( 2 n / 3 )+ O ( n ) , which solves to O ( n log n ) : no good. Why not divide the array into groups of 4? Because odd numbers are convenient (what’s the median of a group of 4 elements?). Why not divide the array into groups of 7? That would work. 5 elements just happens to be the smallest odd number where the recurrence solves to O ( n ) . Incidentally, notice how much simpler the randomized version of this algorithm was? The randomized version also performs better in practice. This example illustrates how randomization can sometimes make algorithms simpler and more efficient—a recurring theme in algorithms.
Image of page 9

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

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture