All lessons

Binary Search

8 min · C++ examples

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

C++ · 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
Goblin Glitch
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.

Stuck? Ask Rena

Your AI tutor that gives assistance.

Asking Rena in lessons is on Ultimate.