What is Binary Search?

Turkish: İkili Arama

Binary search finds a target in sorted data by halving the range each step, aiming for O(log n) lookup time on sorted collections.

Binary search is an efficient search algorithm that works only on sorted data. At each step, the target value is compared with the middle element; if the target is smaller, the left half is searched, and if it is larger, the right half is searched.

How Does It Work?

The algorithm keeps start and end indexes. It calculates the midpoint, compares it with the target, and cuts the search range in half. If the target is found, the index is returned; if the range becomes empty, the value is not present. Because each step halves the search space, time complexity is O(log n).

The requirement is that the data must already be sorted. On an unsorted array, sorting has its own cost, so binary search is not always the most practical option for one-off small lookups.

Business Use

Binary search appears in price-range lookup, date-based record search, sorted ID lists, and low-level library functions. Database indexes are not simply binary search, but sorted tree structures use a similar idea of narrowing the search space. A hash table can provide average O(1) access for equality lookups, but it does not provide the same advantage for ordered range queries.

The most common implementation mistake is using binary search without guaranteeing that the array is sorted.