Why does the worst case of arrayMax cost $8n-2$ while its best case costs $6n$, and which one do we quote?
The two differ by the body of the if: in the worst case every element is a new maximum, so the assignment runs every time; we quote the worst case because it is the only bound that holds for all inputs.
Inside the loop, currentMax ← A[i] only executes when the test succeeds. So:
| Assignment runs | Count | |
|---|---|---|
| Worst case — array is strictly increasing, every element beats the current max | every iteration | $8n - 2$ |
| Best case — the largest element is first, the test never succeeds | never | $6n$ |
Two things are worth noticing.
Why the worst case is the default. Best-case analysis is nearly useless — almost any algorithm looks brilliant on its friendliest input (a sort is "fast" on already-sorted data). The worst case is a guarantee: it is the promise that holds no matter what the user hands you, which is exactly the kind of statement you can build on. Average-case analysis exists too, but it needs an assumption about the input distribution, and a wrong assumption silently invalidates the result.
Why it barely matters here. $8n-2$ and $6n$ are both linear. The best and worst case of this algorithm have the same asymptotic class, so at the level of the O-notation the distinction evaporates. It is only when the two cases differ in shape — quadratic versus linear — that best/worst becomes a decisive question.
Go deeper:
Best, worst and average case — why the worst case is the safe analysis and the average case needs an assumption.