All lessons

Binary Search

8 min · TypeScript examples

Cut a sorted list in half each step to find a value in logarithmic time.

TypeScript · Binary Search0/5 cleared

Enemy 1 of 5

Binary search only works on sorted data. Instead of checking every item, compare your target to the middle element: if it's too small, search the left half; if too large, search the right half.

Each step throws away half the remaining items, so a list of 1,000,000 items needs at most about 20 comparisons.

You
Kernel Hellhound

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: found