3.1.3 · D3Complexity Analysis

Worked examples — Best, worst, average case — with examples

2,391 words11 min readBack to topic

Before any example: a single reminder of the three quantities, in plain words.


The scenario matrix

Every case-analysis problem is really one of these cells. The worked examples below are tagged with the cell they cover, so together they fill the whole grid.

Cell What it stresses Covered by
C1 Best (early exit) luckiest arrangement, loop stops at once Ex 1
C2 Worst (full scan) unluckiest arrangement, target absent Ex 1, Ex 4
C3 Average (uniform position) probability-weighted mean, Gauss sum Ex 2
C4 Degenerate: empty input limiting/edge value, loop never runs Ex 3
C5 Degenerate: single element smallest non-trivial size Ex 3
C6 Ties / duplicates equal keys change which branch runs Ex 5
C7 Rare-bad case (avg midpoint) why average hugs best when bad is rare Ex 6
C8 Limiting behaviour which case dominates asymptotically Ex 7
C9 Real-world word problem translating a story into Ex 8
C10 Exam twist (partial info / unsuccessful-search probability) mixed distribution, don't assume 100% hits Ex 9

Everything below points back to Linear Search, Insertion Sort and Quicksort, and leans on Expected Value and Probability Distributions and Asymptotic Notation (Big-O, Omega, Theta).


Ex 1 — Best & Worst of Linear Search (cells C1, C2)

Forecast: guess before reading. How many comparisons for a value sitting in slot 0? For a value that isn't there at all?

The picture first — the array as a row of boxes and where the search stops.

Figure — Best, worst, average case — with examples
  1. (a) Locate . Why this step? Best case means the cheapest arrangement — an early exit. Here a[0]==7 is true on the first comparison, so the loop returns immediately.
  2. (b) Search . Why this step? Worst case forces the loop to test every slot. never matches, so we compare against all elements, fail every time, then return .

Verify: count the green ticks in the figure — one for (a). Count the red crosses for (b) — five, one per box, matching . ✓ Best , Worst as the pocket table promised.


Ex 2 — Average of Linear Search, the honest way (cell C3)

Forecast: roughly half of 5? A little more? A little less? Write your guess.

  1. Cost when is at index . Why this step? We must know for each arrangement before we can average. If the match is at index (starting from ), we compared against slots — that's comparisons.
  2. Attach probabilities. Why this step? "Equally likely" means for each position — this is the uniform distribution. Average is , not the midpoint of extremes.
  3. Apply the Gauss sum. Why this step? collapses the sum into a formula (pair the smallest with the largest: ).

Verify: list all five costs ; their plain mean is . ✓ Matches . On average we scan a bit past halfway.


Ex 3 — Degenerate sizes: empty and single (cells C4, C5)

Forecast: can an algorithm do zero work? Can best equal worst?

  1. (a) . Why this step? Edge inputs are where formulas either shine or break — you must check them. The loop body for i in 0..n-1 runs zero times because there is no valid . Zero comparisons, straight to return -1.
  2. Sanity-check the formula at the edge. Why this step? Our worst-case formula was ; plugging gives . The successful-average is undefined here — there is no element to succeed on, so "successful average" simply doesn't apply. Good: the edge exposes an assumption.
  3. (b) . Why this step? The smallest non-trivial case: there is only one arrangement, so best, worst and average must all coincide. One comparison against a[0].

Verify: formula at gives , and gives . Both agree with the direct count. ✓ Whenever there is a single possible input, the three cases are forced equal.


Ex 4 — Insertion Sort worst case, reverse-sorted (cell C2)

Forecast: each new element must travel to the very front — will the total look like or like ?

The step figure shows each key sinking all the way left.

Figure — Best, worst, average case — with examples
  1. Count per outer iteration. Why this step? Worst case for insertion sort is reverse order, because every new key is smaller than all before it, so the inner while runs until it falls off the front. Iteration (with ) does exactly comparisons.
  • : insert comparison.
  • : insert comparisons.
  • : insert comparisons.
  1. Sum them. Why this step? Total comparisons is the sum over outer iterations, a triangular number.

Verify: . ✓ Formula grows like — the whole point that insertion-sort worst case is an order above its best case .


Ex 5 — Ties change the branch (cell C6)

Forecast: duplicates — do they cost a lot (like reverse-sorted) or almost nothing?

  1. Evaluate the strict test on equal keys. Why this step? The exact comparison operator decides the branch. With key = 5 and a[j] = 5, the test 5 > 5 is false. So the inner loop stops immediately — no shifting.
  2. One comparison per outer iteration. Why this step? Each of the outer passes does a single failing comparison, exactly like an already-sorted array.

Verify: direct trace — : 5>5 false (1 comp); : 5>5 false (1 comp). Total . ✓ Equal keys hit the best case here only because the operator is strict >. Had it been >=, ties would shift and this would explode to worst case — the operator, not the values, decides.


Ex 6 — Average (best+worst)/2 for Quicksort (cell C7)

Forecast: is the true average nearer the cheap end or the expensive end?

  1. Compute each extreme's magnitude. Why this step? We need concrete numbers to see how far the midpoint sits from reality. With , so :
  2. Form the naive midpoint. Why this step? This is the myth we're debunking.
  3. State the true average. Why this step? The average is ; bad pivots are rare, so their huge cost is multiplied by a tiny probability and barely moves the mean. Quicksort's average is — essentially the best order, not the midpoint.

Verify: ratio of midpoint to true average . ✓ The naive midpoint overestimates by ~50×. Average is a weighted mean; rarity of the bad case drags it down toward best.


Ex 7 — Limiting behaviour, which case wins (cell C8)

Forecast: does the worst/best ratio settle to a constant, or keep growing?

  1. Form the ratio. Why this step? Comparing growth rates is exactly what asymptotics is for — it strips constants and reveals the order gap.
  2. Take the limit. Why this step? The limiting cell asks what happens for huge inputs.

Verify: at the ratio is . ✓ Because the ratio , best and worst are not the same order — an entire factor of apart. This is why saying "insertion sort is " without a case qualifier hides the fast nearly-sorted behaviour.


Ex 8 — Real-world word problem (cell C9)

Forecast: worst near 0.4 s? Average about half that?

  1. Model as linear search. Why this step? One unsorted pass, stop on first match — this is Linear Search. Comparisons ↔ box reads.
  2. (a) Worst-case reads. Why this step? Unluckiest layout: requested box is last, forcing reads.
  3. (b) Average reads. Why this step? Uniform position → average reads .

Verify (units): reads (ms/read) ms. ✓ worst, average — average is close to half the worst, exactly the linear-search story in physical time.


Ex 9 — Exam twist: search that might fail (cell C10)

Forecast: should the answer sit between and ? Where exactly?

  1. List the two outcomes and their costs. Why this step? This is a mixed distribution — the exam trap is assuming every search succeeds. Success cost ; failure cost .
  2. Weight by probability (law of total expectation). Why this step? Overall expected cost , blending the two sub-cases from Expected Value and Probability Distributions.
  3. Compute.

Verify: lies between (all-success) and (all-fail), leaning toward success since . ✓ Check endpoints: at , ; at , — both recovered by the formula.


Recall Which cell did each example fill?

Ex1 ::: C1 best + C2 worst (linear search) Ex2 ::: C3 average, uniform position Ex3 ::: C4 empty input + C5 single element Ex4 ::: C2 worst insertion sort (reverse) Ex5 ::: C6 ties, strict operator matters Ex6 ::: C7 rare-bad, average midpoint Ex7 ::: C8 limiting ratio Ex8 ::: C9 real-world conveyor word problem Ex9 ::: C10 exam twist, mixed success/fail distribution

Connections