Binary Search
EasyAlgorithms
Binary search is deceptively tricky: off-by-one errors are everywhere. Beyond sorted array search, master the generalized template for searching over a monotonic answer space.
AVG TIME
O(log n)
SPACE
O(1)
BEST
O(1)
WORST
O(log n)
Step-by-Step Walkthrough
In Python
Math You Need For This
Imagine a 30-level building with n=1,000,000,000 rooms, one per floor. Binary search: go to floor 500,000,000. Too high? Go to floor 250,000,000. Each elevator ride halves the search space. You find any room in 30 rides.
Required concepts
Key math ideas
1 / 2
Interactive 3D Visualization
Brute Force vs Optimized
Watch It Run
Python Implementation
Now try it yourself
2 challenges with test cases and AI feedback
Practice Now
Complexity Analysis
Binary Search
Next: Recursion & Backtracking