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

  eqnarray37636

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

equation37656

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

  equation37660

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

displaymath69206

which can be rewritten as

  equation37667

Equation gif can be solved by telescoping like this:

   eqnarray37679

Adding together Equation gif through Equation gif gives

eqnarray37725

where tex2html_wrap_inline69234 is the tex2html_wrap_inline69236 harmonic number . Finally, multiplying through by n+1 gives

displaymath69207

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

eqnarray37758

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_inline58403 tex2html_wrap_inline58403 tex2html_wrap_inline58403
quicksort (random pivot selection) tex2html_wrap_inline59127 tex2html_wrap_inline59127 tex2html_wrap_inline58403
Table: Running times for exchange sorting.


next up previous contents index

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