Sub topic 14
Choosing Which Case to Rely on
This chapter assumes you can count basic operations, you know the definitions of best, worst, and average case, and you have seen linear search and insertion sort analyzed. Here you decide which case to use for a given requirement.
Match the case to the requirement
No case is "the right one" in general. Each answers a different question.
| Question you need answered | Case to use | Reason |
|---|---|---|
| Must every request finish within a deadline? | Worst case | It is a guarantee for all inputs of size n. |
| How much total work will a large batch of typical inputs cost? | Average case | Totals over many runs follow the mean. |
| Can some inputs be handled very cheaply? | Best case | It shows what the algorithm does on its easiest inputs. |
Why best case is rarely the deciding case
The best case describes one favorable input. Linear search finds its target at position 0 in 1 comparison, but you cannot plan around that. Use it to understand the shape of the algorithm, or when you can show that your inputs are nearly always favorable. For example, insertion sort on almost sorted data does close to n - 1 comparisons.
When the worst case is the right choice
Choose the worst case when a single slow run is costly. Examples are a real-time controller, a request with a hard timeout, and code that attackers can feed crafted input. The worst case also needs no assumptions about the input, so it is the safest bound to state.
Its weakness is pessimism. If the worst input almost never occurs, the worst-case figure can overstate the cost you will actually see.
When the average case is the right choice
Choose the average case when you run the algorithm many times and the total cost matters more than any one run. The result depends on an assumed input distribution. If the assumption is wrong, the answer is wrong.
Linear search shows this. Averaging over uniformly likely positions gives (n + 1) / 2 comparisons. Suppose instead your data has the target near the front 90% of the time. The real average is much lower, and the uniform formula misleads you.
def linear_search(items, target):
comparisons = 0
for i, x in enumerate(items):
comparisons += 1
if x == target:
return i, comparisons
return -1, comparisons
n = 10
items = list(range(n))
uniform = sum(linear_search(items, t)[1] for t in items) / n
skewed_targets = [0] * 9 + [9] # target is item 0 in 90% of lookups
skewed = sum(linear_search(items, t)[1] for t in skewed_targets) / len(skewed_targets)
print(uniform) # 5.5
print(skewed) # 1.9The same code gives 5.5 or 1.9 depending only on which inputs you assume.
A decision procedure
In practice, report more than one case. A common pairing is the worst case as a guarantee and the average case as the expected behavior. Quicksort is the standard example. It has a poor worst case but a good average case, and that is why people still use it.
Exercise
A web service sorts a small list on every request, and requests must always return within 50 ms. Most lists arrive nearly sorted. Which case should you rely on to decide whether insertion sort is acceptable, and why not the best case?
Show answer
Rely on the worst case. The 50 ms limit applies to every request, so one reversed list taking n(n-1)/2 comparisons could break it. The best case (n - 1 comparisons) describes only the nearly sorted lists. It says nothing about the others. You could add the average case, with the "mostly sorted" distribution stated, to estimate typical load.