Best, Worst, and Average Case
When discussing the efficiency of a program or algorithm, we need to distinguish between the worst case, the average case, and the best case.
Here, for example, is a summary of the asymptotic running times for four sorting algorithms:
worst case | average case | best case | |
---|---|---|---|
selection sort | Θ(n2) | Θ(n2) | Θ(n2) |
insertion sort | Θ(n2) | Θ(n2) | Θ(n) |
quicksort | Θ(n2) | Θ(n lg n) | Θ(n lg n) |
merge sort | Θ(n lg n) | Θ(n lg n) | Θ(n lg n) |
Selection sort always takes Θ(n2) time because it compares each of the n elements to be sorted against each of the other n-1 elements.
The other three algorithms in that table have a best case that avoids most of those comparisons. In their best case, they rely on the transitivity of total orders to avoid comparing x against z when the algorithm has already determined that x is less than some y that is less than z.
Avoiding computations whose outcome is implied by previous computations is another way to compute less.
We will illustrate that by proving the correctness of a quicksort program. We will then analyze the best and worst case for that program.