Cover 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_inline68935. Similarly, the average the value of tex2html_wrap_inline68937. To determine the average running time, we rewrite Equation gif thus:

  eqnarray37385

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

equation37405

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

  equation37409

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

displaymath68923

which can be rewritten as

  equation37416

Equation gif can be solved by telescoping like this:

   eqnarray37428

Adding together Equation gif through Equation gif gives

eqnarray37474

where tex2html_wrap_inline68951 is the tex2html_wrap_inline68953 harmonic number . Finally, multiplying through by n+1 gives

displaymath68924

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

eqnarray37507

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_inline58122 tex2html_wrap_inline58122 tex2html_wrap_inline58122
quicksort (random pivot selection) tex2html_wrap_inline58846 tex2html_wrap_inline58846 tex2html_wrap_inline58122
Table: Running times for exchange sorting.


next up previous contents index

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