Binary Search Algorithm in 100 Seconds
Channel: Fireship
Full video link:
https://www.youtube.com/watch?v=MFhxShGxHWcAlgorithm • 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) |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Binary Search Algorithm in 100 Seconds
Channel: Fireship
Full video link:
https://www.youtube.com/watch?v=MFhxShGxHWcWatch how low/high boundaries shrink while finding a target in a sorted array.
Start with the full search range.
Array
[1, 3, 5, 7, 9, 11, 13, 15]
Target
11
Low / High / Mid
0 / 7 / 3 (value 7)
Decision
7 is too small, move low to mid + 1.
Learn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Low/High bounds
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.
Midpoint
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.
Monotonic condition
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.
Boundary policy
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.
Termination condition
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.
Monotonic
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.
Lower bound
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.
Upper bound
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.
Search on answer
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.
Invariant
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.
This walkthrough connects the core concepts of Binary Search into one end-to-end execution flow.
Step 1
Current active interval.
Before
Sorted
After
Search range now
Transition
Why this step matters: Low/High bounds is a required building block for understanding how Binary Search stays correct and performant on large inputs.
Step 2
Candidate index/value chosen each iteration.
Before
Range values
After
Transition
Why this step matters: Midpoint is a required building block for understanding how Binary Search stays correct and performant on large inputs.
Step 3
Rule that decides left or right half.
Before
After
Transition
Why this step matters: Monotonic condition is a required building block for understanding how Binary Search stays correct and performant on large inputs.
Step 4
Choose first true, last true, exact match, etc.
Before
After
Transition
Why this step matters: Boundary policy is a required building block for understanding how Binary Search stays correct and performant on large inputs.
vs Linear scan
When to choose this: Choose binary search when sorted/monotonic structure exists.
Tradeoff: Requires setup conditions and careful boundary logic.
vs Hash lookup
When to choose this: Choose binary search when sorted order/range queries matter.
Tradeoff: Hash map is faster for exact-key average lookup.
vs Balanced tree
When to choose this: Choose binary search over arrays for read-heavy static datasets.
Tradeoff: Trees handle dynamic inserts/deletes better.
Search infrastructure uses binary-search-style lookups across sorted index segments and posting lists.
Takeaway: Logarithmic probing remains core at web scale.
Netflix
Capacity and quality threshold tuning often applies binary search on monotonic metrics.
Takeaway: Binary search helps optimize parameters quickly.
Databases
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.
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
}
Use these signals to decide if this data structure/algorithm is the right fit before implementation.
O(n) to O(log n) when monotonic order exists.| # | 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) |