nextupprevious
Next:9.7 Order StatisticsUp:9.6 Quick SortPrevious:9.6.2 Algorithm for Partitioning

9.6.3 Quicksort: Average Case Analysis

Assume that all initial orderings of the keys are equally likely; Also, since the pivot is the larger of the first two elements, left groups tend to be larger than the right groups.

The left group can have i elements where i = 1, 2,..., n - 1, since the left group has at least one element and the right group also has at least one element.

Let us fix i and try to compute the probability:

                                                P{ left group has i elements}

Now, left group has i elements

$ \Rightarrow$pivot must be the (i + 1)stelement among the n elements
 

If the pivot is in position 1, then the element in position 2 is one of the i smaller elements and vice-versa.

P{ Position 1 contains one of the i smaller elements }

                                                                    = $\displaystyle \left(\vphantom{\frac{i}{n-1}}\right.$$\displaystyle {\frac{i}{n-1}}$$\displaystyle \left.\vphantom{\frac{i}{n-1}}\right)$$\displaystyle \left(\vphantom{\frac{1}{n}}\right.$$\displaystyle {\frac{1}{n}}$$\displaystyle \left.\vphantom{\frac{1}{n}}\right)$
Similarly,

P{ Position 2 contains one of the i smaller elements}

                                                                     = $\displaystyle \left(\vphantom{\frac{1}{n}}\right.$$\displaystyle {\frac{1}{n}}$$\displaystyle \left.\vphantom{\frac{1}{n}}\right)$$\displaystyle \left(\vphantom{\frac{i}{n-1}}\right.$$\displaystyle {\frac{i}{n-1}}$$\displaystyle \left.\vphantom{\frac{i}{n-1}}\right)$
Therefore,

P{ left group has i elements} 

                                                                    = $\displaystyle {\frac{2i}{n(n-1)}}$
This leads to

                                    T(n$\displaystyle \leq$C2n$\displaystyle \sum_{i=1}^{n-1}$$\displaystyle {\frac{2i}{n(n-1)}}$$\displaystyle \left\{\vphantom{ T(i) + T(n-i) }\right.$T(i) + T(n - i)$\displaystyle \left.\vphantom{ T(i) + T(n-i) }\right\}$
Using
$\displaystyle \sum_{i=1}^{n-1}$f (i) = $\displaystyle \sum_{i=1}^{n-1}$f (n - i),
we get
                                            T(n$\displaystyle \leq$C2n$\displaystyle {\frac{1}{n-1}}$$\displaystyle \sum_{i=1}^{n-1}$$\displaystyle \left\{\vphantom{ T(i) + T(n-i) }\right.$T(i) + T(n - i)$\displaystyle \left.\vphantom{ T(i) + T(n-i) }\right\}$
The above expression is in the form it would have been if we had picked a truly random pivot at each step.

The above simplifies to:
                                            T(n$\displaystyle \leq$C2n$\displaystyle {\frac{2}{n-1}}$$\displaystyle \sum_{i=1}^{n-1}$T(i)
The above is the recurrence that one would get if all sizes between 1 and n - 1for the left group were equally likely. Thus picking the larger of the two elements doesn't really affect the size distribution.

We shall guess the solution
                                           T(n$\displaystyle \leq$Cnlog n
for some constant C and prove its correctness using induction.

Basis:

                                    n = 2 $\displaystyle \Rightarrow$Cnlog n = 2C
 

which is correct.

Induction Step:

AssumeT(i$ \leq$Cilogi$ \forall$i < n.

T(n) $\displaystyle \leq$ C2n$\displaystyle {\frac{2C}{n-1}}$$\displaystyle \sum_{i=1}^{n-1}$ilog i
     
  $\displaystyle \leq$ C2n$\displaystyle {\frac{2C}{n-1}}$$\displaystyle \sum_{i=1}^{n/2}$ilogi$\displaystyle {\frac{2C}{n-1}}$$\displaystyle \sum_{i=\frac{n}{2} + 1}^{n-1}$ilog i
     
  $\displaystyle \leq$ C2n$\displaystyle {\frac{2C}{n-1}}$$\displaystyle \sum_{i=1}^{n/2}$ilog$\displaystyle {\frac{n}{2}}$$\displaystyle {\frac{2C}{n-1}}$$\displaystyle \sum_{i=\frac{n}{2} + 1}^{n-1}$ilog n
     
  $\displaystyle \leq$ C2n + Cnlogn$\displaystyle {\frac{Cn}{4}}$$\displaystyle {\frac{Cn}{2(n-1)}}$,simplification  

PickingC$ \geq$ 4C2, we have,

                                C2n$\displaystyle {\frac{Cn}{4}}$$\displaystyle \geq$ 0
Thus T(n) is O(nlog n)


nextupprevious
Next:9.7 Order StatisticsUp:9.6 Quick SortPrevious:9.6.2 Algorithm for Partitioning
eEL,CSA_Dept,IISc,Bangalore