This page is due for review. Re-read the key points and answer the self-check questions before recording your video.
Algorithms · Hub
Searching
Seven ways to find something in an array. Linear search works on anything in $O(n)$. The others use order (sorted, rotated, evenly spread, or hill-shaped) to throw away big parts of the array with each comparison and reach $O(\log n)$ or better.
Why it matters: the question "what do I know about my data?" decides the speed. The same lookup can take a million comparisons or twenty.
5 random questions mixed from all seven searches.
Mixed review
Answer each one out loud before opening the sample answer.
flowchart TD
A{"Looking for a value, or a peak?"} -->|"a peak / maximum of a hill"| P{"Array or function?"}
P -->|array| PF["Peak finding"]
P -->|"unimodal function"| T["Ternary search"]
A -->|"a value"| B{"Is the data sorted?"}
B -->|no| L["Linear search"]
B -->|"sorted but rotated"| R["Rotated array search"]
B -->|yes| C{"Size unknown, or target near the front?"}
C -->|yes| E["Exponential search"]
C -->|no| D{"Values evenly spread?"}
D -->|yes| I["Interpolation search"]
D -->|"no / not sure"| BS["Binary search"]
Key points
Linear search is the only one with no data requirement, because it never assumes anything about unchecked elements.
Binary, exponential and rotated-array search are $O(\log n)$ because order lets one comparison discard half the range.
Interpolation search can reach $O(\log \log n)$ because it guesses the position from the value, but only on evenly spread data.
Ternary search loses to binary search on arrays because its 2 comparisons per round outweigh the bigger cut; it wins on hill-shaped functions.
Peak finding is $O(\log n)$ on unsorted data because it only needs a local peak, and the uphill side always has one.