Divide-and-conquer search on sorted data
Binary search is the textbook logarithmic pattern: each comparison discards half of the remaining candidates. Sorted NumPy arrays, database indexes, and monotonic time series all justify O(log n) lookups where a linear scan would be O(n).
The idea transfers to data science in less obvious places: finding breakpoints in piecewise-constant calibration curves, bisection root-finding for monotone likelihoods, or locating insertion positions for quantile buckets. The invariant is always: the answer lies in one of two halves, and you can decide which half in constant time.
Iterative binary search (two indices, while lo <= hi) avoids recursion depth entirely and is what most engineers ship. Recursive formulations are fine for teaching when n is small.
bisect module (binary search helpers): [1].
Sources
Tasks
Card Info
- Topic: Recursion & structural decomposition
- Difficulty: Intermediate
- Completed: 0 users