Sub topic 8
Best, worst, and average case
This chapter assumes you can read loops and nested loops for their cost, and that you know Big O is an upper bound and Big Omega is a lower bound. Here we separate two ideas that beginners often merge: the kind of bound (O, Ω, Θ) and the kind of input (best, worst, average).
One algorithm, many inputs
The cost of an algorithm often depends on the input's contents, not only its size. Fix the size n and the cost can still vary. Case analysis picks a specific family of inputs of size n and asks how expensive the algorithm is on that family.
- Best case: the input of size n that makes the algorithm cheapest.
- Worst case: the input of size n that makes it most expensive.
- Average case: the expected cost over a stated distribution of inputs of size n.
Each case gives its own cost function of n. You can then describe any of those functions with O, Ω, or Θ.
Worked example: linear search
def linear_search(items, target):
comparisons = 0
for i, x in enumerate(items):
comparisons += 1
if x == target:
return i, comparisons
return -1, comparisons
data = list(range(1, 101))
print(linear_search(data, 1)) # (0, 1): best case, found first
print(linear_search(data, 100)) # (99, 100): found last
print(linear_search(data, 0)) # (-1, 100): absent, worst caseThe table gives the number of comparisons for a list of n items.
| Case | Input | Comparisons | Tight bound |
|---|---|---|---|
| Best | Target is the first item | 1 | Θ(1) |
| Worst | Target is last or absent | n | Θ(n) |
| Average | Target present, each position equally likely | (n + 1) / 2 | Θ(n) |
For the average row, a target at position k costs k comparisons. Averaging 1 through n gives (n + 1) / 2, and dropping the constant factor leaves Θ(n).
Cases are not bounds
A common error is to say "Big O is the worst case, Big Omega is the best case." That is not correct. Cases and bounds are independent choices.
- The worst case of linear search is Θ(n). You can also say it is O(n²), which is true but loose.
- The best case of linear search is Θ(1). It is also Ω(1) and O(n).
- The overall running time of linear search is O(n) and Ω(1). No single Θ describes every input.
Quote the case together with the bound: "worst-case O(n)" or "best-case Ω(1)". When people say only "linear search is O(n)", they usually mean the worst case.
Which case should you use?
- Worst case gives a guarantee. Use it when you need a hard limit, as in real-time systems or code that handles untrusted input.
- Average case predicts typical behavior. Use it when the distribution is realistic and occasional slow runs are acceptable.
- Best case is rarely useful alone. Its main value is showing when an algorithm can exit early.
Quicksort is the standard example. Its worst case is O(n²), but its average case is O(n log n), and that is why it is widely used.
Exercise
A function scans a list of n distinct items for a target that is guaranteed to be present. Every position is equally likely. How many comparisons does it make in the best, worst, and average case, and what is the tight bound for each?
Show answer
Best case: 1 comparison, Θ(1). Worst case: n comparisons, Θ(n). Average case: (n + 1) / 2 comparisons, which is Θ(n).
Exercise
A colleague says "the best case is the Big Omega and the worst case is the Big O." Give one example showing why this is wrong.
Show answer
Linear search has a worst-case running time of Θ(n). That worst case is therefore both O(n) and Ω(n). A bound type can describe any case, so each case has its own O, Ω, and Θ.