🧮Linear Search
Also known as Sequential Search
Checking each item in turn until you find a match.
Think of it like
Think of it like looking for a sock. You open each drawer, one at a time.
Example
Jack looks for 9 in the list 4, 7, 1, 9. Linear search checks four numbers and finds it at position 3.
Why it matters
It is simple and always works. Knowing its cost tells you when to reach for something faster.
Definition
Example code
python
>>> [4, 7, 1, 9].index(9)
3Common mistake
Key takeaways
- 1One by one. From the start.
- 2Any list. Sorted or not.
- 3O(n). Slow on big lists.