P1. Cases
Linear search returns the index of the first occurrence of target in \(A[0..N-1]\), or \(-1\) if it is absent.
- Describe a best-case input. How many comparisons?
- Describe two worst-case inputs with different return values.
- Why is “about \(N/2\), so \(\Theta(1)\)” wrong?
Show the answer
Best: target sits in \(A[0]\). One comparison. \(\Theta(1)\) for that input only.
Worst, found: target equals \(A[N-1]\), return \(N-1\). Worst, missing: target is absent, return \(-1\). Both take \(N\) comparisons.
\(N/2\) is still linear. \(\Theta(1)\) means the cost stops growing with \(N\).