Algorithms
Binary Search
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Divide and conquer search algorithm |
| Time | O(log n) |
| Space | O(1) iterative, O(log n) recursive |
| Prerequisite | Array must be sorted |
| Best For | Finding elements in sorted arrays, finding boundaries |
One-liner: Halves the search space each step by comparing with the middle element.
๐ฎ Interactive Visualizer
Watch how Binary Search eliminates half the search space with each comparison:
Loading visualizer...
Try these operations:
- Search for an element that exists - count the comparisons
- Search for an element that doesn't exist - see where it would be
- Notice: array of 1000 elements needs only ~10 comparisons!
๐ง Key Implementations
Basic Binary Search
JAVA(18 lines)CodeLoading syntax highlighter...
Find First Occurrence (Lower Bound)
JAVA(16 lines)CodeLoading syntax highlighter...
Find Last Occurrence (Upper Bound)
JAVA(16 lines)CodeLoading syntax highlighter...
Java Standard Library
JAVA(10 lines)CodeLoading syntax highlighter...
๐ Complexity Analysis
| Scenario | Comparisons | Time |
|---|---|---|
| n = 10 | ~3-4 | O(log n) |
| n = 100 | ~6-7 | O(log n) |
| n = 1,000 | ~10 | O(log n) |
| n = 1,000,000 | ~20 | O(log n) |
| n = 1,000,000,000 | ~30 | O(log n) |
Why O(log n)?
Each step halves the search space: n โ n/2 โ n/4 โ n/8 โ ... โ 1 Number of steps = logโ(n) Example: 1024 elements 1024 โ 512 โ 256 โ 128 โ 64 โ 32 โ 16 โ 8 โ 4 โ 2 โ 1 = 10 steps = logโ(1024)
โ When to Use Binary Search
Good Use Cases
- Searching sorted arrays - classic use case
- Finding boundaries - first/last occurrence, insertion point
- Searching in rotated arrays - modified binary search
- Finding peak elements - search space reduction
- Optimization problems - "find minimum X such that..."
Avoid When
- Unsorted data - must sort first, O(n log n)
- Small datasets - linear search is simpler for n < 10-20
- Frequent insertions - keeping array sorted is expensive
- Linked lists - no O(1) random access
๐งฉ Common Patterns
Search in Rotated Sorted Array
JAVA(28 lines)CodeLoading syntax highlighter...
Find Peak Element
JAVA(15 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Integer Overflow
JAVA(5 lines)CodeLoading syntax highlighter...
2. Off-by-One Errors
JAVA(17 lines)CodeLoading syntax highlighter...
3. Forgetting Sort Requirement
JAVA(7 lines)CodeLoading syntax highlighter...
๐ค Interview Tips
Q: What's the time complexity of binary search?
"O(log n) - we eliminate half the search space with each comparison. For an array of n elements, we need at most logโ(n) + 1 comparisons.
Q: Can you implement binary search without recursion?
"Yes, the iterative version is actually preferred as it uses O(1) space vs O(log n) stack space for recursion.
Q: How do you handle duplicates?
"Use lower_bound/upper_bound variants. Lower bound finds the first occurrence, upper bound finds the position after the last occurrence. Count of element = upper_bound - lower_bound.
Q: When is linear search better than binary search?
"For very small arrays (n < 10-20), linear search can be faster due to simpler logic and better cache behavior. Also when array is unsorted and will only be searched once.
๐ Series Navigation
Previous: Part 8: EnumSet
Visualizer:
BinarySearch from @tomaszjarosz/react-visualizers