Average case analysis of randomized quicksort

1 / 10

# Average case analysis of randomized quicksort - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Average case analysis of randomized quicksort' - zeheb

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
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
• 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
• 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
• 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?
• 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
• 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
• Since each pair is compared only once, the total number of comparisons performed by the whole quicksort run is
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