Foundation · Module 1 · Lesson 2

Best, average, and worst case

How the same algorithm behaves across different inputs.

15–25 min readLesson 2 of 123

Lesson 2 of 123

1d streak

Detailed theory

A single algorithm can perform different amounts of work on different inputs of the same size. Case analysis describes that variation; asymptotic notation describes the growth within a chosen case.

By the end, you can

  • Construct best-case and worst-case inputs.
  • Understand why average case needs an input distribution.
  • Avoid confusing input cases with O, Ω, and Θ.

Vocabulary

Definitions with concrete meaning

Best case

The minimum cost among all valid inputs of size n.

Example: Linear search finds the target at index 0: Θ(1).

Worst case

The maximum cost among all valid inputs of size n.

Example: Linear search misses the target or finds it last: Θ(n).

Average case

The expected cost across inputs under a stated probability distribution—not merely (best + worst) / 2.

Example: If a present target is equally likely at any index, linear search performs (n + 1) / 2 comparisons: Θ(n).

Expected complexity

An expectation over randomness in the algorithm, the input, or both; the source of randomness must be stated.

Example: Randomized quicksort has expected Θ(n log n) time for any fixed input.

Rules to apply

  1. 1Hold input size n fixed while comparing different arrangements or values.
  2. 2Define exactly what counts as success, failure, and a primitive operation.
  3. 3Worst-case bounds give guarantees; average-case bounds need probability assumptions.
  4. 4A case can have a tight Θ bound: best-case Θ(1), worst-case Θ(n).
  5. 5Do not use Ω as a synonym for best case or O as a synonym for worst case.

Worked example 1

Linear search across all three cases

Search an unsorted array of n values from left to right.

for i = 0 to n - 1:
  if a[i] == target: return i
return -1
  1. 1

    Best input

    Target is at index 0, so one comparison is made: Θ(1).

  2. 2

    Worst input

    Target is absent or last, so n comparisons are made: Θ(n).

  3. 3

    Average assumption

    Assume the target exists and each index is equally likely. Expected comparisons are (1 + 2 + … + n) / n = (n + 1) / 2.

The average is Θ(n), even though the best case is constant.

Worked example 2

Why quicksort has different cases

Partition around a pivot, then recursively sort both sides.

  1. 1

    Balanced partitions

    Two subproblems of about n/2 create log n levels with Θ(n) work per level.

  2. 2

    Highly unbalanced partitions

    Subproblem sizes n−1 and 0 create n levels, each doing a shrinking linear partition.

  3. 3

    State the result

    Balanced: Θ(n log n). Repeated worst pivots: Θ(n²).

Random pivot selection protects the expected runtime, but it does not remove the quadratic worst case.

Comparison table

Case comparison

Always read each cell as a bound for the named case and assumptions.

Algorithm / operationBestAverage / expectedWorst
Linear searchΘ(1)Θ(n)Θ(n)
Binary searchΘ(1)Θ(log n)Θ(log n)
Insertion sortΘ(n)Θ(n²)Θ(n²)
Randomized quicksortΘ(n log n)Expected Θ(n log n)Θ(n²)
Hash-table lookupΘ(1)Expected Θ(1)Θ(n)

Check your understanding

1Is the average case always halfway between best and worst?

No. It is a weighted expectation over inputs and requires a probability distribution.

2Does Ω(n) mean “best-case linear”?

No. Ω is a lower-bound notation and can describe any selected case. Always name the case separately.

3Why do engineers often emphasize worst case?

It supplies a guarantee, which matters for latency limits, real-time systems, and adversarial inputs.

Cost Model

Complexity & Trade-offs

Linear search — best
Θ(1)
Θ(1)
The target is at the first position.
Linear search — average
Θ(n)
Θ(1)
Under uniform successful positions, expected comparisons are (n + 1) / 2.
Linear search — worst
Θ(n)
Θ(1)
The target is absent or at the final position.

When to use it

  • Choosing between expected speed and hard latency guarantees.
  • Understanding behavior on sorted, random, duplicated, or adversarial inputs.
  • Explaining randomized algorithms and hash-table performance accurately.

Common mistakes

  • Calculating average case as (best + worst) / 2.
  • Using O, Ω, and Θ as replacements for worst, best, and average case.
  • Claiming expected performance without identifying the source of randomness.

Practice prompts

  • Analyze linear search when the target is absent with probability 1/2.
  • Construct best and worst inputs for insertion sort.
  • Explain expected versus worst-case lookup in a hash table.

Mastery check

Can you derive it instead of memorizing it?

State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.