Binary search

 is a common technique to search for specific targets in arrays of sorted data.



Basic principles for binary search

 * 1) Make sure that the search interval strictly decreases after each iteration.
 * 2) Make sure that you do NOT eliminate an element that can possibly be the correct target(s).

Examples

 * Classical binary search
 * Search in a sorted 2D matrix
 * Closest element to target
 * Find first and last position of element in sorted array
 * First target in sorted array
 * Last target in sorted array
 * Closest k elements
 * K-th closest element
 * Smallest element that is larger than target
 * K-th smallest number in two sorted arrays
 * Median of two sorted arrays
 * Binary search with unknown size
 * First bad version