# Binary Search

> Finding an item in a sorted list. Each step cuts the search in half.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** Half-Interval Search, Bisection Search

## Think of it like

Think of it like a phone book. Open it in the middle, then pick a half.

## Definition

Binary search finds an item in a sorted list by cutting the search in half each step. It checks the middle item first. If the target is smaller, it throws away the right half; if bigger, the left half. A list of 1,000,000 items needs at most 20 checks, so it is O(log n). It only works when the list is already sorted.

## Example

Lucy searches 1,000,000 sorted names for one name. Binary search finds it in at most 20 checks.

## Example code

```python
>>> import bisect
>>> bisect.bisect_left([2, 5, 8, 12, 20], 12)
3
```

bisect halves the sorted list until it finds where 12 sits.

## Why it matters

It turns a search of millions into a handful of steps. Databases and many libraries use it inside.

## Common mistake

Running it on a list that is not sorted. It gives a wrong answer without any warning.

## Three principles

- **Sorted lists only.** Or the answer is wrong.
- **Halve each step.** Check the middle.
- **O(log n).** 20 checks for a million.

## Related terms

- [Linear Search](https://learn.benibix.com/term/development/linear-search)
- [Binary Search Tree (BST)](https://learn.benibix.com/term/development/binary-search-tree)
- [Big O Notation](https://learn.benibix.com/term/development/big-o-notation)
- [Divide and Conquer](https://learn.benibix.com/term/development/divide-and-conquer)

## Told apart from

- [Linear Search vs Binary Search](https://learn.benibix.com/compare/linear-search-vs-binary-search): Linear search checks every item, one by one, and works on any list. Binary search needs a sorted list, but halves it each step, so a million items take about 20 checks.

---

Source: https://learn.benibix.com/term/development/binary-search
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
