We Need To Talk About Binary Search.
We Need To Talk About Binary Search. (canhazcode.blogspot.co.uk)
Binary search is the classical algorithm which is easy to describe, but hard to implement well.
It’s a basic divide and conquer algorithm to find the position of an element within a sorted list: Pick a pivot element in the centre of the list, and either the pivot matches, or the element must be above, or below it in the list. Repeat ad nauseum and return the position, if found
The article suggests an elegant reframing of binary search: Instead of thinking of it as searching for the position of an element within a sorted list, they break it into two problems: searching for the first position, or final position of an element in a sorted list. As a result, the code is much simpler to reason about.