Skip to content
BENIBIX Learn

Best, Worst and Average Case

Development🧮 Algorithms & Data StructuresAlgorithm Basics

Also known as Worst Case, Best Case, Average Case, Worst-Case Complexity

How fast code runs on the best input, the worst one and a normal one.

Think of it like

It is like looking for your shoes. Some days they are by the door, some days they are lost.

Example

A linear search over 1,000 names finds the first name in 1 step. It needs 1,000 steps for the last one or a missing one.

Why it matters

A sort that is quick on most data can still freeze on a bad input. The worst case tells you how bad it can get.

Definition

The same algorithm can be fast or slow, depending on the input. The best case is the luckiest input. The worst case is the unluckiest one. The average case is what to expect over many normal inputs. Big O is most often given for the worst case, because that is the promise you can count on.
One search, three cases. Found at once, found halfway, or every box checked.

Common mistake

Quoting the best case as if it were normal. Plan for the worst case unless you know your data well.

Key takeaways

  1. 1
    Best case. The luckiest possible input.
  2. 2
    Worst case. The slowest it can ever be.
  3. 3
    Average case. What to expect most days.