Analysis of Quicksort
We'll cover the following...
How is it that quicksort's worst-case and average-case running times differ? Let's start by looking at the worst-case running time. Suppose that we're really unlucky and the partition sizes are really unbalanced. In particular, suppose that the pivot chosen by the partition function is always either the smallest or the largest element in the n-element subarray. Then one of the partitions will contain no elements and the other partition will contain
Worst-case running time
When quicksort always has the most unbalanced partitions possible, then the original call takes
When we total up the partitioning times for each level, we get:
The last line is because
Best-case running time
Quicksort's best case occurs when the partitions are as evenly balanced as possible: their sizes either are equal or are within