CSAlgorithms & ComplexityDiagramBeginner
Binary Search
KO: 이진 탐색·EN: Binary Search·JA: 二分探索
A divide-and-conquer search algorithm that halves a sorted range each step to locate a target or boundary in O(log N) time
Table of Contents
Structure Diagram
Binary Search O(log N) vs Linear Search O(N) Comparisons
(40, 16)Binary Search O(log N) #3
Quick Answer
What is Binary Search?
A divide-and-conquer search algorithm that halves a sorted range each step to locate a target or boundary in O(log N) time. It is a core concept in Algorithms & Complexity within CS.
Core Principles
Binary Search locates a target or threshold in a sorted array or monotonic predicate space by checking the midpoint and discarding half of the remaining range on every iteration.
O(log₂ N)Efficiency: Searching 1 million items takes at most 20 comparisons; 1 billion items takes only ~30.- Parametric Search: Beyond finding exact values, binary search solves optimization problems by finding the smallest or largest
xwherecheck(x)flips from false to true.
Practical Takeaways
- Avoid midpoint overflow: Compute
lo + ((hi - lo) >> 1)instead of(lo + hi) / 2in fixed-width integer languages. - Prefer half-open
[lo, hi)bounds: Usinglower_bound/upper_boundconventions cleanly handles duplicates and prevents off-by-one infinite loops. - Amortize sorting cost: Binary search requires sorted order—if writes are frequent, use a hash table or balanced BST instead of re-sorting.
Notable Examples
git bisect: Pinpointing the exact commit that introduced a regression inO(log N)test runs.- Database Page Lookups: Locating keys inside sorted B+Tree leaf pages.
- Capacity Planning: Finding the minimum rate limit or cluster size that satisfies an SLA.