Find a target in a sorted array by halving the search space each step. Β β± Time
O(log n)Β πΎ SpaceO(1)
If the array is sorted, you can eliminate half the remaining elements at each step. Check the middle element β if it equals the target you're done; if the target is larger, discard the left half; if smaller, discard the right half. Repeat until found or the search space is empty.
Algorithm:
left, right = 0, len(array) - 1
while left <= right:
mid = (left + right) // 2
if array[mid] == target: return mid
elif target > array[mid]: left = mid + 1
else: right = mid - 1
return -1 # not found
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Target: 9
L R
Step 1: mid=5 β value=6 β 9>6 β move L right
L R
Step 2: mid=8 β value=9 β FOUND at index 8 β
Only 2 comparisons for 10 elements!
(vs. up to 10 comparisons with linear search)
Binary search was first described by John Mauchly in 1946. Despite its simplicity, a bug-free implementation is notoriously tricky β Jon Bentley noted in 1986 that most textbook implementations had bugs. The modern left + (right - left) // 2 form avoids integer overflow.
- Array must be sorted β if not, sort first (adds O(n log n))
- Use
left <= right(not<) to handle single-element arrays correctly - Prefer
mid = left + (right - left) // 2in languages with integer overflow (Python is fine with//) - Know the 3 variants: exact match, find first occurrence, find insert position
- Binary search applies beyond arrays: search on answer space (e.g., "minimum speed to finish tasks")
- Searching in sorted databases / dictionaries
- Finding the insert position (
bisectmodule in Python) - Rotated sorted array search (LeetCode 33)
- "Minimum/maximum feasible value" problems (binary search on answer)
- Square root / power approximations
- Merge Sorted β maintains sorted order
- Longest Increasing Subsequence β O(n log n) variant uses binary search