Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
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_inline70219. Similarly, the average the value of tex2html_wrap_inline70221. To determine the average running time, we rewrite Equation gif thus:

  eqnarray37899

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

equation37919

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

  equation37923

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

displaymath70207

which can be rewritten as

  equation37930

Equation gif can be solved by telescoping like this:

   eqnarray37942

Adding together Equation gif through Equation gif gives

eqnarray37988

where tex2html_wrap_inline70235 is the tex2html_wrap_inline70237 harmonic number . Finally, multiplying through by n+1 gives

displaymath70208

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

eqnarray38021

Table gif summarizes the asymptotic running times for the quicksort routine 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_inline59179 tex2html_wrap_inline59179 tex2html_wrap_inline59179
quicksort (random pivot selection) tex2html_wrap_inline59897 tex2html_wrap_inline59897 tex2html_wrap_inline59179
Table: Running Times for Exchange Sorting


next up previous contents index

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