Big O Notation

Is Big O Notation Always the Worst Case?

Big O is not always about the worst case. Learn what Big O measures, how best, average and worst cases differ, and when each one matters in practice.

On this page
  1. Short answer
  2. The three cases
  3. A linear search example
  4. Quicksort, the classic example
  5. Why the distinction matters
  6. Amortized bounds
  7. Summary

Short answer

No. Big O is a way to write an upper bound on how an operation’s cost grows with its input. It does not have to describe the worst case. You can state Big O for the best case, the average case or the worst case, and each answers a different question.

Textbooks often write a bound without naming the case. When no case is given, the worst case is often what is meant, which is why the question comes up so often.

The three cases

  • Best case: the input that makes the algorithm do the least work.
  • Average case: the expected work over the inputs you might see, given some assumption about how those inputs are distributed.
  • Worst case: the input that makes the algorithm do the most work.

A linear search example

Suppose you search an unsorted list of n items for one value.

  • Best case: the value is the first item, so the search stops after one comparison. That is O(1).
  • Worst case: the value is last or missing, so every item is checked. That is O(n).
  • Average case: if the value is equally likely to be in any position, the search checks about n/2 items on average. Dropping the constant 1/2 still gives O(n).

Binary search on a sorted array tells a different story. Its best case is O(1), when the middle item is the target, while its worst and average cases are both O(log n).

Quicksort, the classic example

Quicksort shows the gap between cases clearly.

  • Average case: with reasonable pivot choices, the work is O(n log n).
  • Worst case: if each pivot splits off almost nothing, for example by always choosing the first element of an already sorted list, the work becomes O(n²).

Anyone asking “What is the most efficient sorting algorithm?” should check which case matters for their data, because quicksort’s answer depends on it.

Why the distinction matters

Hash tables show the same pattern. A lookup is O(1) on average but can be O(n) in the worst case, when many keys collide. An unbalanced binary search tree can degrade to O(n) lookups, while self-balancing trees keep the worst case at O(log n). The avl tree in data structure courses is one example, since its rebalancing exists to keep that worst case in check.

If a system has to respond within a fixed time on every call, the worst case is the number to watch. If an occasional slow operation is acceptable, the average case may be a better guide.

Amortized bounds

Some bounds average over a sequence of operations rather than over random inputs. Appending to a dynamic array is O(1) amortized: an occasional resize copies every item, but spread over many appends, the cost per append stays constant on average. This is a different averaging idea from the average case above.

Summary

Big O is a notation for growth, and it can describe any case. A bare claim often means the worst case, but the clearest way to write it is to name the case: best, average or worst.

The complete guide to big o notation

Big O for Data Structures
Read the guide

Keep exploring

Guides from other topics

All topics →