Binary search finds something in a sorted range by checking the middle and throwing away the half that can't contain the answer. Then it repeats on the half that's left. Each step cuts the problem in half, so even a million items take only about 20 steps.
The guessing game
"I'm thinking of a number between 1 and 100." The smart strategy is to guess 50. If I say "higher", you know it's 51 to 100, so you guess 75. Then 63, 57, and so on. Each guess halves what's left. You'll always find it in 7 guesses or fewer.
That's binary search. The array needs one property: when you look at the middle, you must be able to tell which half the answer is in. A sorted array gives you that for free.
Why it's so fast: O(log n)
Each step halves the range. How many halvings until one item is left?
Items (n) | Steps (about log₂ n) |
|---|---|
| 8 | 3 |
| 1,000 | 10 |
| 1,000,000 | 20 |
| 1,000,000,000 | 30 |
That's O(log n): the work barely grows even when the input explodes. Compare O(n), which would take a billion steps for the last row.
The template
let left = 0
let right = nums.length - 1
while (left <= right) {
const mid = Math.floor((left + right) / 2) // the middle index
if (nums[mid] === target) return mid // found it
if (nums[mid] < target)
left = mid + 1 // answer is in the right half
else right = mid - 1 // answer is in the left half
}
return -1 // not foundWalking through it
nums = [1, 3, 5, 7, 9, 11, 13], looking for 9:
left | right | mid | nums[mid] | Decision |
|---|---|---|---|---|
| 0 | 6 | 3 | 7 | 7 < 9 → left = 4 |
| 4 | 6 | 5 | 11 | 11 > 9 → right = 4 |
| 4 | 4 | 4 | 9 | found at index 4 ✅ |
The real requirement: "which half?"
Binary search doesn't strictly need a perfectly sorted array. It needs a way to look at the middle and decide which half to keep. That's why it works on a rotated sorted array (the two problems here): you can't use plain "smaller or bigger" anymore, but with one extra comparison you can still tell which half to keep.
| Problem | How you decide which half to keep |
|---|---|
| Find Minimum in Rotated Sorted Array | Is nums[mid] bigger than the last element? Then the drop is right. |
| Search in Rotated Sorted Array | Find the half that's sorted normally, and check if the target fits. |
Two loop styles
There are two common versions, and mixing them up causes infinite loops:
while (left <= right)withleft = mid + 1andright = mid - 1: for "find this exact value". The range can become empty, which means "not found".while (left < right)withleft = mid + 1andright = mid: for "find the first position where something becomes true" (like the minimum). It stops when one candidate is left, which is the answer.
Pick one and keep the updates consistent with it.
How to recognize this category
Think "binary search" when:
- the input is sorted (or sorted with a twist, like rotated)
- the problem demands O(log n)
- you're looking for the first or last position where some condition flips from false to true
- you can guess an answer and quickly check "too small" or "too big"
Common mistakes
- Off-by-one errors.
right = midvsright = mid - 1matters. Ask: "couldmiditself be the answer?" If yes, keep it. - Infinite loops. With
while (left < right)andleft = mid(without+ 1), a two-element range never shrinks. - Forgetting the array must be sorted. Binary search on an unsorted array gives wrong answers, silently.