Skip to content
Part IA Lent Term

Worst-Case, Average-Case, and Best-Case Analysis

Three Kinds of Analysis

For an input size nn, define the cost function C(x)C(x) for a specific input instance xx:

  • Worst-case: W(n)=maxx:x=nC(x)W(n) = \max_{x: |x|=n} C(x) — the maximum cost over all inputs of size nn.
  • Best-case: B(n)=minx:x=nC(x)B(n) = \min_{x: |x|=n} C(x) — the minimum cost over all inputs of size nn.
  • Average-case: A(n)=E[C(x)]A(n) = \mathbb{E}[C(x)] — the expected cost, assuming some probability distribution over inputs.

Insertion Sort as Canonical Example

Recall the cost expression for Insertion Sort:

T(n)=an+(b+c+g)(n1)+dj=2ntj+(e+f)j=2n(tj1)T(n) = an + (b+c+g)(n-1) + d\sum_{j=2}^{n} t_j + (e+f)\sum_{j=2}^{n} (t_j - 1)

where tjt_j is the number of WHILE-loop iterations for iteration jj. The value of tjt_j depends on how far A[j]A[j] must travel into the sorted prefix.

Best Case (Θ(n)\Theta(n))

Input is already sorted. Every tj=1t_j = 1 (only the failing comparison at loop entry). The sums collapse to linear terms, giving T(n)=linear function=Θ(n)T(n) = \text{linear function} = \Theta(n).

Worst Case (Θ(n2)\Theta(n^2))

Input is reverse-sorted. Each jj requires moving j1j-1 elements (plus the final failing comparison, so tj=jt_j = j):

j=2ntj=j=2nj=n(n+1)21\sum_{j=2}^{n} t_j = \sum_{j=2}^{n} j = \frac{n(n+1)}{2} - 1

j=2n(tj1)=j=2n(j1)=n(n1)2\sum_{j=2}^{n} (t_j - 1) = \sum_{j=2}^{n} (j-1) = \frac{n(n-1)}{2}

Both sums are Θ(n2)\Theta(n^2), so T(n)=Θ(n2)T(n) = \Theta(n^2).

Average Case (Θ(n2)\Theta(n^2))

Assume all permutations of the input are equally likely. On average, half the elements in A[1j1]A[1 \dots j-1] are smaller than A[j]A[j] and half are larger, so the inner loop runs j/2j/2 times. This yields the same arithmetic progression with a factor of 1/21/2, still Θ(n2)\Theta(n^2). The average case is often the same order of magnitude as the worst case.

Why Worst-Case Matters

  1. Guarantees: An O(n2)O(n^2) worst-case bound means the algorithm will never exceed that asymptotic cost for any input. This is essential for real-time and safety-critical systems.
  2. Adversarial inputs: Worst cases may occur in practice. A human sorting a list is unlikely to provide reverse-sorted input, but programmatic data sources might.
  3. Average case often equals worst case: For many algorithms (including Insertion Sort), the average and worst cases are the same asymptotic order, so there is no reason to use a weaker analysis.

Analysis vs Benchmarking

Asymptotic AnalysisBenchmarking
Predicts behaviour for all input sizesMeasures behaviour on specific inputs
Machine-independent (RAM model)Machine-dependent (CPU, cache, OS)
Ignores constantsCaptures real constants
Good for algorithm comparisonGood for implementation tuning

The RAM model analysis is useful for algorithmic decisions (MergeSort over Insertion Sort for large nn). Benchmarks become useful for small nn, where constants dominate.

Summary

CaseInsertion SortDefinition
BestΘ(n)\Theta(n)Already-sorted input
WorstΘ(n2)\Theta(n^2)Reverse-sorted input
AverageΘ(n2)\Theta(n^2)Random permutation, each element moves ~i/2i/2 positions
Why worstProvides guarantees, often similar to average