Binary Search

Finds target in sorted array by halving search range each step.

Method

On a sorted array, set low = 0 and high = n − 1; inspect mid = low + floor((high − low) / 2), then discard the half that cannot contain the target. Return an index or not-found. O(log n) comparisons.

Applications

Databases, libraries, debugging, search engines.

Advantages

Logarithmic: 1B items in ~30 steps.

Limitations

Requires sorted data; unsorted data must be sorted first.

Example

Searching 1M sorted IDs takes at most 20 steps vs 1M linear steps.

Sources

Educational reference only, not professional advice.

All algorithms