Linear Search
The simplest search: walk the array front to back until the target appears. No preconditions — works on unsorted data — but every miss costs a full scan.
- Category
- Searching
- Time complexity
- O(n)
- Space complexity
- O(1)
Pseudocode
for i ← 0 to n−1 if a[i] ≠ target: continue return i return not found
Reference implementation
def linear_search(a, t):
for i, v in enumerate(a):
if v == t:
return i
return -1