272. When do you use linear search versus binary search?
EasySearching & selection
Technical
Show model answer
Linear search works on any sequence, sorted or not, including linked lists, in O(n) time and O(1) space. It is often fastest for tiny arrays and is the only option for unsorted data you search once.
Binary search requires sorted, randomly accessible data and runs in O(log n) by halving the range on every comparison. Sorting costs O(n log n), so it pays off when you search the same data many times. Common bugs are overflow in `(lo + hi) / 2` (use `lo + (hi − lo) / 2` in fixed-width languages) and off-by-one loop bounds.