Binary search is everywhere, you just don't see it
·2 min read ·Computer Science · Algorithms · Debugging
Binary search is the first 'real' algorithm most developers learn in a computer science course. Then they implement it once, pass the exam, and forget about it because it seems like an academic exercise.
It shows up everywhere once you know what to look for.
The Classic Version
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
O(log N). Searching one billion sorted records takes about 30 iterations.
In Your Database
B-tree indexes are binary search trees. When you run WHERE id = 12345 on an indexed column, the database doesn't scan the table—it binary searches the index. This is why indexes transform O(N) scans into O(log N) lookups. You use binary search every time you query an indexed column.
In Git Bisect
git bisect start
git bisect bad # Current commit is broken
git bisect good v1.2.0 # This version was fine
# Git checks out the midpoint commit
git bisect good # Or: git bisect bad
# Repeat until Git identifies the breaking commit
git bisect is literally binary search over your commit history. Given N commits, it finds the breaking change in log(N) steps.
The Generalized Pattern
Any time you have a monotonic property—'everything before X is good, everything after is bad'—binary search applies. Finding the right configuration value, the right threshold, the commit that introduced a regression. The pattern is the same: eliminate half the search space at each step.
Algorithms aren't academic. They're patterns that show up in real systems.