Binary Search Algorithm in 100 Seconds
Channel: Fireship
Watch on YouTube →Algorithm • medium
Binary search repeatedly halves a sorted search space, making it one of the highest-impact logarithmic-time techniques.
It applies beyond arrays: any monotonic predicate can often be solved with binary search on answer space.
The hard part is boundary correctness, not the core idea.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Search | O(log n) |
See It
A small animated SVG that illustrates the core idea the moving parts you need to keep in your head when you read the rest of the guide.
Each comparison kills off half of the remaining candidates, giving log₂(n) work.
Watch First
A quick visual walkthrough before the detailed sections pick the format that helps you learn fastest.
Channel: Fireship
Watch on YouTube →Mechanics
The building blocks and terminology you need before everything else clicks. Skim or deep-read.
What it is Current active interval.
Why it matters Low/High bounds is a required building block for understanding how Binary Search stays correct and performant on large inputs.
What it is Candidate index/value chosen each iteration.
Why it matters Midpoint is a required building block for understanding how Binary Search stays correct and performant on large inputs.
What it is Rule that decides left or right half.
Why it matters Monotonic condition is a required building block for understanding how Binary Search stays correct and performant on large inputs.
What it is Choose first true, last true, exact match, etc.
Why it matters Boundary policy is a required building block for understanding how Binary Search stays correct and performant on large inputs.
What it is Loop invariant proving progress each step.
Why it matters Termination condition is a required building block for understanding how Binary Search stays correct and performant on large inputs.
What it is Condition changes in one direction only over domain.
Why it matters Condition changes in one direction only over domain. In Binary Search, this definition helps you reason about correctness and complexity when inputs scale.
What it is First index where condition becomes true.
Why it matters First index where condition becomes true. In Binary Search, this definition helps you reason about correctness and complexity when inputs scale.
What it is First index greater than target.
Why it matters First index greater than target. In Binary Search, this definition helps you reason about correctness and complexity when inputs scale.
What it is Binary search over value range instead of array index.
Why it matters Binary search over value range instead of array index. In Binary Search, this definition helps you reason about correctness and complexity when inputs scale.
What it is Property that remains true across loop iterations.
Why it matters Property that remains true across loop iterations. In Binary Search, this definition helps you reason about correctness and complexity when inputs scale.
Walkthrough
Every frame of the concept diagram, laid out top-to-bottom so you can scroll through the algorithm at your own pace.
Initial range [0, 15]. Target = 29.
mid = (0+15)/2 = 7. arr[7] = 23. 23 < 29 → discard left half.
New range [8, 15].
mid = (8+15)/2 = 11. arr[11] = 35. 35 > 29 → discard right half.
New range [8, 10].
mid = (8+10)/2 = 9. arr[9] = 29 → found at index 9.
Tradeoffs
When this technique wins, when it loses, and what to reach for instead.
Choose this when Choose binary search when sorted/monotonic structure exists.
Tradeoff Requires setup conditions and careful boundary logic.
Choose this when Choose binary search when sorted order/range queries matter.
Tradeoff Hash map is faster for exact-key average lookup.
Choose this when Choose binary search over arrays for read-heavy static datasets.
Tradeoff Trees handle dynamic inserts/deletes better.
In Production
Where this shows up at real companies and what the on-call engineer learned the hard way.
Search infrastructure uses binary-search-style lookups across sorted index segments and posting lists.
Takeaway Logarithmic probing remains core at web scale.
Capacity and quality threshold tuning often applies binary search on monotonic metrics.
Takeaway Binary search helps optimize parameters quickly.
B-tree pages and sorted blocks rely on binary search to locate keys in each node/page.
Takeaway Storage engines depend on reliable boundary-search behavior.
Code
A clean reference implementation in TypeScript, Python, and Go. Switch languages with the toggle.
Classic exact lookup over ascending sorted array.
Complexity: Time O(log n), Space O(1)
Binary search exact target
function binarySearch(values: number[], target: number): number {
let lowIndex = 0
let highIndex = values.length - 1
while (lowIndex <= highIndex) {
const midIndex = lowIndex + Math.floor((highIndex - lowIndex) / 2)
const midValue = values[midIndex]
if (midValue === target) return midIndex
if (midValue < target) lowIndex = midIndex + 1
else highIndex = midIndex - 1
}
return -1
}
Watch Out
The bugs and edge cases that bite engineers most often. Skim before you ship.
Pro Tips
Small habits that pay off every time you reach for this pattern.
Pattern Recognition
Concrete signals that tell you this is the right pattern both at work and on LeetCode.
O(n) to O(log n) when monotonic order exists.Practice
A curated easy-to-hard problem ladder. Each one reinforces a specific aspect of the pattern.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Binary Search | Easy | O(log n) |
| 2 | Search Insert Position | Easy | O(log n) |
| 3 | First Bad Version | Easy | O(log n) |
| 4 | Find First and Last Position | Medium | O(log n) |
| 5 | Search in Rotated Sorted Array | Medium | O(log n) |
| 6 | Find Peak Element | Medium | O(log n) |
| 7 | Koko Eating Bananas | Medium | O(n log m) |
| 8 | Median of Two Sorted Arrays | Hard | O(log(min(m,n))) |
| 9 | Split Array Largest Sum | Hard | O(n log m) |
| 10 | Find in Mountain Array | Hard | O(log n) |