Skip to main content

Binary Search

Efficiently finds a target in a sorted array by repeatedly halving the search range.

Interactive Visualization​

Loading visualization…

Usage​

src/sample/binarysearch.ts
import { binarySearch } from 'athro';

const arr = [2, 4, 6, 8];
const target = 6;

const result = binarySearch(arr, target);
Caution

The array must be sorted. Binary search only works on sorted arrays.

Time Complexity​

CaseTime ComplexityDescription
BestO(1)Target found at the middle index
AverageO(log n)Target found after halving the range
WorstO(log n)Target at an edge or not present