nextupprevious
Next:9.8 Lower Bound on Complexity for Sorting MethodsUp:9.7 Order StatisticsPrevious:9.7.2 Algorithm 2

9.7.3 Algorithm 3

Worst case linear time algorithm due to

Blum, Floyd, Pratt, Rivest, Tarzan.

REF.
Manuel Blum, Robert W. Floyd, Vaughan Pratt, Ronald L Rivest, and Robert E. Tarjan. Time bounds for selection. Journal of Computer and System Sciences, Volume 7, Number 4, pp. 448-461, 1973.
1.
Divide the n elements into groups of 5 leaving aside between 0 and 4 elements that cannot be placed in a group.

 

 

Sort each group of 5 elements by any algorithm and take the middle element from each group. This yields

                                                                                                        $ \lfloor$n/5$ \rfloor$ medians.

2.
Use the SELECT algorithm to find the median of these $ \lfloor$n/5$ \rfloor$elements. Choose this median as the pivot.
                                                        3$\displaystyle \lfloor$$\displaystyle {\frac{n-5}{10}}$$\displaystyle \rfloor$ elements.                                         3$\displaystyle \lfloor$$\displaystyle {\frac{n-5}{10}}$$\displaystyle \rfloor$$\displaystyle \geq$ 3$\displaystyle \lfloor$$\displaystyle {\textstyle\frac{70}{10}}$$\displaystyle \rfloor$ = 21 > $\displaystyle {\textstyle\frac{75}{4}}$ Implementation

keytype select (int i, j, k);

        /* returns key of the kth largest element among A[i ...  A[j]*/

        {

            if((j - i) < 75)

                find the kth largest by some simple algorithm

            else {

                for(m = 0;m$ \leq$ (j - i - 4)/5;m + +)

            {

                find the third element among A[i + 5 * m ...  A[i + 5 * m + 4]

                and swap it with A[i + m];

                pivot = select (i,(j - i - 4)/5,(j - i - 4)/10)

                m = partition (i, j, pivot);

                       if(k$ \leq$m - i)

                            return (select (i, m - 1, k))

                     else

                            return (select (m, j,(k - (m - i)))

            }

        }

}

The worst case complexity of the above algorithm can be described by

T(n) $\displaystyle \leq$ C1  if n$\displaystyle \leq$ 75
  $\displaystyle \leq$ C2n + T$\displaystyle \left(\vphantom{\frac{n}{5}}\right.$$\displaystyle {\frac{n}{5}}$$\displaystyle \left.\vphantom{\frac{n}{5}}\right)$ + T$\displaystyle \left(\vphantom{\frac{3n}{4}}\right.$$\displaystyle {\frac{3n}{4}}$$\displaystyle \left.\vphantom{\frac{3n}{4}}\right)$  if n > 75  

Let us guess the solution T(n) = Cn for n > 75. For n$ \geq$ 75, assumeT(m$ \leq$Cm for m < n. Then

T(n) $\displaystyle \leq$ C2n$\displaystyle \left(\vphantom{\frac{Cn}{5}}\right.$$\displaystyle {\frac{Cn}{5}}$$\displaystyle \left.\vphantom{\frac{Cn}{5}}\right)$$\displaystyle \left(\vphantom{\frac{3Cn}{4}}\right.$$\displaystyle {\frac{3Cn}{4}}$$\displaystyle \left.\vphantom{\frac{3Cn}{4}}\right)$
     
  $\displaystyle \leq$ C2n$\displaystyle {\textstyle\frac{19}{20}}$Cn  

Thus T(n) is O(n).


nextupprevious
Next:9.8 Lower Bound on Complexity for Sorting MethodsUp:9.7 Order StatisticsPrevious:9.7.2 Algorithm 2
eEL,CSA_Dept,IISc,Bangalore