Lesson 23 / 42
Linear Search
Check every element until you find the target. O(n), works on any data.
The baseline
Walk left to right, comparing each item. Best case O(1) (first slot), worst and average O(n). No sorting or extra memory required — the fallback when nothing else applies.
Implementation
Return the index, or -1 if absent.
def linear_search(arr, target):
for i, x in enumerate(arr):
if x == target:
return i
return -1When it's the right call
Small or unsorted data, linked lists (no random access), or when you'd spend more sorting than searching once.