Visual walkthrough — Best, worst, average case — with examples
Step 1 — What we are counting
WHAT. We have an array — a row of boxes — holding items. We call it a, and its boxes are numbered . We are hunting for one value, the key, written . The rule: read box , then box , and so on left-to-right, and stop the instant we find .
WHY. Before we can average anything, we must agree on the single thing we tally. That thing is one comparison — the yes/no test "is the value in this box equal to ?". Every other cost (moving the finger, checking the loop) rides along with a comparison, so counting comparisons captures the running time.
PICTURE. The finger lands on box and asks its question. If "no", it slides right.

Step 2 — The two extremes we already understand
WHAT. Two arrangements are easy. If sits in box , we stop after one comparison. If sits in the last box (or is not present at all), we make comparisons.
WHY. These are the [!definition] best and worst cases from the parent note. We show them first because the average must land somewhere between them — they are the guardrails that sanity-check our final answer.
PICTURE. Green finger stops at once (best). Red finger walks the whole row (worst).

Here reads "best cost for size "; reads "worst cost for size ". Notice both are just evaluated at the luckiest () and unluckiest () box.
Step 3 — Why we cannot use one number
WHAT. For a fixed size , the cost is not one value — it is a whole list of possible values: , one for each place the key could hide.
WHY. This is the heart of best/worst/average: the size is frozen, but the layout is free. A single number would be a lie. So we ask a fairer question — "if I do this search many, many times, what is the typical cost?"
PICTURE. A bar for each possible resting place, its height equal to the cost . The heights climb like a staircase.

Step 4 — Attaching a probability to each outcome
WHAT. To average, we must say how likely each resting place is. Our model (the "successful search, uniform" model from the parent) says: the key is present, and it is equally likely to be in any one of the boxes.
WHY. "Average" is meaningless until you name a distribution — this is exactly what Expected Value and Probability Distributions insists on. Equally likely is the honest default when we know nothing extra about the data.
PICTURE. Every box wears the same probability tag .

Step 5 — What "expected value" means, as a picture
WHAT. The expected value (or average case ) is: multiply each possible cost by its probability, then add everything up.
WHY. Why this tool — a weighted sum — and not the plain midpoint ? Because a plain midpoint ignores how often each outcome actually happens. The weighted sum is the mathematically honest "typical value". (For linear search the two happen to agree — but for Quicksort they wildly disagree, which is exactly why we build the honest tool.)
PICTURE. Each staircase bar from Step 3, now shrunk by its weight ; the average is where the whole shrunken pile balances.

Step 6 — Doing the sum with Gauss's trick
WHAT. Plug and into the expected-value sum and simplify.
WHY. The is the same for every term, so it slides out front — cleaning up the sum. Then relabel : as runs , runs . Now we owe just "add up ."
PICTURE — Gauss pairing. Pair the first with the last (), the second with the second-last ()… every pair equals , and there are pairs.

Feed it back:
The 's cancel, leaving the clean . In Asymptotic Notation (Big-O, Omega, Theta) this is — the same order as the worst case, because half of a big row is still a big row.
Step 7 — The degenerate cases (never leave a gap)
WHAT & WHY. A formula you trust must survive the weird inputs.
PICTURE. Three tiny arrays: empty, single box, and a failed search.

The one-picture summary
Everything at once: the staircase of costs, the uniform weights , the Gauss pairing, and the balance point landing at between the best and worst guardrails.

Recall Feynman retelling — say it back in plain words
I'm looking for one card in a face-down row of cards, flipping left to right until I hit it. Counting: flipping until the card at position costs flips. Extremes: front card = flip (lucky), back card or missing = flips (unlucky). The catch: with fixed, the cost is a whole staircase , not one number — so I ask for the typical cost. Fairness rule: I assume the card is equally likely anywhere, so each spot gets weight (and those weights add to , so it's legal). Averaging: typical cost = sum of (weight × cost) = . Gauss magic: pairs up into pairs each worth , giving ; the eats one , leaving — I scan about half the row on average. Edges: one card → ; empty → formula sits out; card absent → always . Same row length, but luck decides the flips.
Connections
- Parent: 3.1.03 Best, worst, average case — with examples (Hinglish) — same three cases, in Hinglish.
- Linear Search — the algorithm dissected here.
- Expected Value and Probability Distributions — the weighted-sum machinery of Step 5.
- Asymptotic Notation (Big-O, Omega, Theta) — why is .
- Insertion Sort and Quicksort — where average and worst diverge far more dramatically.
- Amortized Analysis — averaging over a sequence, a different kind of average.