Logo Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Average Running Time

To determine the average running time for the quicksort algorithm, we shall assume that each element of the sequence has an equal chance of being selected for the pivot. Therefore, if i is the number of elements in a sequence of length n less than the pivot, then i is uniformly distributed in the interval [0,n-1]. Consequently, the average value of tex2html_wrap_inline69031. Similarly, the average the value of tex2html_wrap_inline69033. To determine the average running time, we rewrite Equation gif thus:

  eqnarray37465

To solve this recurrence we consider the case n>2 and then multiply Equation gif by n to get

equation37485

Since this equation is valid for any n>2, by substituting n-1 for n we can also write

  equation37489

which is valid for n>3. Subtracting Equation gif from Equation gif gives

displaymath69019

which can be rewritten as

  equation37496

Equation gif can be solved by telescoping like this:

   eqnarray37508

Adding together Equation gif through Equation gif gives

eqnarray37554

where tex2html_wrap_inline69047 is the tex2html_wrap_inline69049 harmonic number . Finally, multiplying through by n+1 gives

displaymath69020

In Section gif it is shown that tex2html_wrap_inline63598, where tex2html_wrap_inline63600 is called Euler's constant . Thus, we get that the average running time of quicksort is

eqnarray37587

Table gif summarizes the asymptotic running times for the quicksort method and compares it to those of bubble sort. Notice that the best-case and average case running times for the quicksort algorithm have the same asymptotic bound!

 

 

running time

algorithm

best case average case worst case
bubble sort tex2html_wrap_inline58202 tex2html_wrap_inline58202 tex2html_wrap_inline58202
quicksort (random pivot selection) tex2html_wrap_inline58926 tex2html_wrap_inline58926 tex2html_wrap_inline58202
Table: Running times for exchange sorting.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.