Starting on page 156 of cormen et al
This presentation is the property of its rightful owner.
Sponsored Links
1 / 10

Average case analysis of randomized quicksort PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

Starting on page 156 of Cormen, et al. Average case analysis of randomized quicksort. Running time is dominated by partition. The running time is dominated by the running time in partition procedure Each time partition is called, a pivot element is picked

Download Presentation

Average case analysis of randomized quicksort

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Starting on page 156 of cormen et al

Starting on page 156 of Cormen, et al.

Average case analysis of randomized quicksort


Running time is dominated by partition

Running time is dominated by partition

  • The running time is dominated by the running time in partition procedure

  • Each time partition is called, a pivot element is picked

  • This element is never included in any future recursive calls to quicksort and partition


Calculating the total time

Calculating the total time

  • One call to partition takes O(1) time plus an amount of time that is proportional to the number of iterations of the for loop in lines 3-6

  • Each iteration performs a comparison in line 4, comparing the pivot element with another element of the array A

  • Therefore, if we can count the total number of times that line 4 is executed, we can bound the total time spent in the for loop during the entire execution of quicksort


Lemma 7 1

Lemma 7.1

  • Let X be the number of comparisons performed in line 4 of partition over the entire execution of quicksort on an n-element array

  • Then the running time of quicksort is O(n+X)

  • Proof: the n is from the total number of calls to partition, each requiring a constant number of processing plus the for loop. However, we charge the time to do the for loops to the X term. An “accounting” trick!!


Computing x

Computing X

  • If we can compute X we’re done

  • To do this we must understand when the algorithm compares two elements of the array and when it doesn’t

  • For ease of analysis, let’s rename the elements of the array A as z1, z2….zn with zi being the ith smallest element.

  • Also define set Zij = {zi, zi+1, …, zj}


When does the algorithm compare zi to zj

When does the algorithm compare zi to zj?

  • First observe that each pair of elements is compared at most once

  • That’s because elements are only compared to the pivot element in any particular call to partition

  • That pivot element will never be the pivot element again in any subsequent call to partition


Using indicator random variables

Using indicator random variables

  • Our analysis will now use indicator random variables

  • Define Xij = I{zi is compared to zj}

  • Where we are considering whether the comparison takes place at any time during the execution of the whole quicksort algorithm, not just during any one call to partition


Total time

Total time

  • Since each pair is compared only once, the total number of comparisons performed by the whole quicksort run is


Computing e x

Computing E[X]


When is zi compared to zj

When is zi compared to zj?

  • Only when either zi or zj is the first element within Zij to be picked as a pivot

  • That is, if any other element in that interval had been picked first, then zi and zj would never be compared

  • So Xij = pr{zi is compared to zj = 2/(j-i+1)

  • Summing this give an upper bound of n ln n


  • Login