3.1.3Complexity Analysis

Best, worst, average case — with examples

1,673 words8 min readdifficulty · medium2 backlinks

WHY do we need three cases?


HOW to compute each case (recipe)


Search for key xx in array a[0..n-1], scanning left to right, stop on first match.

for i in 0..n-1:
    if a[i] == x: return i     # success
return -1                       # not found

Best case. Why this step? The cheapest arrangement makes the loop stop immediately — a[0] == x. One comparison. B(n)=1Θ(1)B(n) = 1 \in \Theta(1)

Worst case. Why this step? The most expensive case forces scanning everything: xx is at the last slot, or absent. We do nn comparisons. W(n)=nΘ(n)W(n) = n \in \Theta(n)

Average case (successful search, xx equally likely at each position). Why this step? If xx is at index ii (prob 1n\tfrac1n), it costs i+1i+1 comparisons. Take the expectation: A(n)=i=0n11n(i+1)=1nk=1nk=1nn(n+1)2=n+12Θ(n)A(n) = \sum_{i=0}^{n-1} \frac{1}{n}\,(i+1) = \frac{1}{n}\sum_{k=1}^{n} k = \frac{1}{n}\cdot\frac{n(n+1)}{2} = \frac{n+1}{2} \in \Theta(n) Why this step? k=1nk=n(n+1)2\sum_{k=1}^n k = \tfrac{n(n+1)}{2} (Gauss pairing). So on average we scan about half the array.


Worked Example 2 — Insertion Sort (comparison count)

for i in 1..n-1:
    key = a[i]
    j = i-1
    while j>=0 and a[j] > key:   # comparison + shift
        a[j+1] = a[j]; j -= 1
    a[j+1] = key

Best case — already sorted. Why this step? If the array is ascending, the while condition a[j] > key is false on the first test every time. Each of the n1n-1 outer iterations does exactly 1 comparison. B(n)=n1Θ(n)B(n) = n-1 \in \Theta(n)

Worst case — reverse sorted. Why this step? Each new key is smaller than everything before it, so the inner loop walks all the way to the front. Iteration ii does ii comparisons: W(n)=i=1n1i=(n1)n2Θ(n2)W(n) = \sum_{i=1}^{n-1} i = \frac{(n-1)n}{2} \in \Theta(n^2)

Average case (random permutation). Why this step? For element ii, on average it sinks halfway into the sorted prefix, costing i/2\approx i/2 comparisons: A(n)i=1n1i2=12(n1)n2=n(n1)4Θ(n2)A(n) \approx \sum_{i=1}^{n-1}\frac{i}{2} = \frac{1}{2}\cdot\frac{(n-1)n}{2} = \frac{n(n-1)}{4} \in \Theta(n^2)

Figure — Best, worst, average case — with examples

Worked Example 3 — Why average ≠ "best+worst over 2"


Big-O / Big-Θ vs the three cases


What does best-case complexity measure?
The minimum number of operations over all inputs of a fixed size nn.
What does worst-case complexity measure?
The maximum operations over all inputs of fixed size nn — the unluckiest arrangement.
Define average-case formally.
A(n)=IPr(I)T(I)A(n)=\sum_I \Pr(I)\,T(I), the expected cost under an assumed input distribution.
In best/worst/average, what is held constant?
The input size nn; only the input's arrangement/content varies.
Linear search: best, worst, average comparisons?
11, nn, n+12\frac{n+1}{2} respectively.
Insertion sort best vs worst case order?
Best Θ(n)\Theta(n) (already sorted), worst Θ(n2)\Theta(n^2) (reverse sorted).
Why is "average = (best+worst)/2" wrong?
Average is probability-weighted; midpoint ignores how rare/common extreme inputs are (e.g. quicksort avg is Θ(nlogn)\Theta(n\log n), not midpoint with Θ(n2)\Theta(n^2)).
Is Big-O the same as worst case?
No. O/Ω/Θ are bound types; best/worst/average are which input you consider — they're independent.
Sum formula used in these derivations?
k=1nk=n(n+1)2\sum_{k=1}^{n} k = \frac{n(n+1)}{2}.
Recall Explain it to a 12-year-old

You're looking for your friend in a line of kids. If she's right at the front, you find her instantly — that's the best case. If she's at the very back (or not in line!), you check everyone — that's the worst case. If you do this many times and average how many kids you check, it's usually about half the line — that's the average case. Same line length, but luck changes how long it takes!

Connections

  • Asymptotic Notation (Big-O, Omega, Theta) — the bound language used to express each case.
  • Linear Search and Insertion Sort — primary worked examples here.
  • Quicksort — average vs worst divergence; randomized pivots.
  • Expected Value and Probability Distributions — foundation of average-case analysis.
  • Amortized Analysis — a different "averaging" (over an operation sequence, not over inputs).

Concept Map

needs 3 answers

min over inputs

max over inputs

expected value

requires

illustrated by

match at a0

last or absent

equally likely position

Gauss sum gives

corrects

Running time for size n

Fix input size n

Best case B of n

Worst case W of n

Average case A of n

Assumed input distribution

Linear Search example

Myth: worst = big n

n plus 1 over 2

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek hi algorithm same size ke alag-alag inputs pe alag time le sakta hai. Isliye hum sirf "n elements me kitna time" nahi bolte — hum batate hain kaunse input pe. Yahin se aate hain teen cases: best (sabse lucky arrangement, sabse kam operations), worst (sabse unlucky, sabse zyada operations), aur average (typical random input pe expected operations). Important baat: teeno me nn same rehta hai, sirf input ka arrangement badalta hai — yeh mat samajhna ki best matlab chhota nn aur worst matlab bada nn.

Linear search se samjho: agar element pehle slot pe mil gaya to 1 comparison (best). Agar last me ya hai hi nahi to poore nn comparisons (worst). Random query pe average n+12\frac{n+1}{2}, yaani lagbhag aadhi list. Insertion sort aur dramatic hai — already sorted array pe Θ(n)\Theta(n) (best), reverse sorted pe Θ(n2)\Theta(n^2) (worst). Isiliye nearly-sorted data ke liye insertion sort accha maana jaata hai.

Ek common galti: log sochte hain "average = (best + worst) ka aadha". Galat! Average ek probability-weighted mean hota hai, Pr(I)T(I)\sum \Pr(I)\,T(I). Quicksort dekho — best aur average dono Θ(nlogn)\Theta(n\log n) hain, lekin worst Θ(n2)\Theta(n^2), kyunki bure pivots kabhi-kabhi hi aate hain. Aur yaad rakho: Big-O, Omega, Theta sirf bound ke types hain; woh best/worst/average se alag cheez hain — dono ko mix mat karna.

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections