Reqflow
Searching·Beginner

Binary Search

Time O(log n)Space O(1)
·

Binary search finds a value in a sorted array by repeatedly cutting the search space in half. Each step you look at the middle element and ask one question: is the target bigger or smaller? The answer lets you discard half the remaining elements at once, which is why even a million-element array is found in about twenty steps.

Example: Find 23 in a sorted array of 10 numbers.

When to use this

See also

← All algorithms