2025-01-12
Written by: Juri Breslauer
Algorithm Notes
Binary search works only on sorted data. At each step it checks the middle value and removes the half that cannot contain the target.
Interactive demo
Binary search keeps two borders: low and high. The target can only be inside
that range.
mid = Math.floor((low + high) / 2).low to the right.high to the left.Binary search has O(log₂n) time complexity because every step removes half of
the remaining values. A list of one million sorted items takes about 20 checks,
not one million.
Here are examples of the same binary search algorithm in different programming languages.