25812162338567291
ALGORITHMS.ANIMATED · DIVIDE & CONQUER

Binary Search Explained with Animations

The fastest way to search a sorted array — explained through stunning animations. Watch the search space halve every step, try it yourself, and see why it's O(log n).

// WHAT YOU'LL LEARN

🔍What is Binary Search?
✂️Halving the Search Space
O(log n) Complexity
Interactive Searcher
⚖️Linear vs Binary
📋Properties & Facts
Beginner·15 min·Algorithms · Divide & Conquer
Reviewed by Reviewed by CodeTikki Editorial Team

// DEFINITION

What is
binary search?

Binary search is an algorithm for finding a target in a sorted array. It looks at the middle element. If the middle is the target, done! If the target is smaller, throw away the right half. If larger, throw away the left half. Repeat on the remaining half.

Each step cuts the search space in half. So for an array of size n, you need only about log₂(n) steps — that's blazingly fast even for billions of elements.

LINEAR SEARCH — SLOW
A
B
C
D
E
F
G
S
A ≠ S
B ≠ S
C ≠ S
... check every name ...
✗ Up to N comparisons
BINARY SEARCH — FAST
A
B
C
D
E
F
G
S
Mid = D. S > D → go right
Mid = G. S > G → go right
Mid = S. Found! ✓
✓ Only ~log₂(N) comparisons

// WATCH IT IN ACTION

Binary search,
step by step.

Pick a target below and press play. Watch how the search space halves every step until the target is found — or proven missing. The narration explains each move.

📖

Imagine a phone book!

You want to find "Smith" in a 1000-page phone book. You don't flip page by page from the start — that would take forever! Instead, you open the book in the MIDDLE. S comes after M, so you tear the book in half and throw away the front. Open the middle again... repeat. Each time you throw away HALF the pages. That's binary search!

FIND →
▶️

Press PLAY to start the search!

// READY
0
2
1
5
2
8
3
12
4
16
5
23
6
38
7
56
8
72
9
91
10
108
11
145
12
200
13
256
14
333
MID (checking)
IN RANGE
DISCARDED
FOUND ✅
Comparisons made
...

// HOW IT WORKS — STEP BY STEP

1️⃣STEP 01

Find the middle

Look at the element in the middle of the current search range (lo to hi).

2️⃣STEP 02

Compare to target

Is the middle equal to, less than, or greater than the target?

3️⃣STEP 03

Discard a half

Equal → done! Smaller → search the right half. Larger → search the left half.

STEP 04

Repeat or finish

Keep halving until you find the target, or lo > hi (not present).

💡

Why is it O(log n)?

Each step halves the search space: n → n/2 → n/4 → ... → 1. The number of halvings to reach 1 is log₂(n). So for n = 1,000,000, that's about 20 steps. For n = 1,000,000,000, only ~30. The search space collapses exponentially fast.

// FLOWCHART · ALGORITHM VISUALIZATION

// FLOWCHART · Binary Search
Search for target = 23 in a sorted array of 10 elements
array = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] · target = 23
NoYesYesNoYesNoStartlo ← 0hi ← n - 1lo ≤ hi ?mid ← lo +(hi - lo) / 2array[mid]= target ?array[mid]< target ?hi ← mid - 1return -1return midlo ← mid + 1

Press PLAY to trace the algorithm through the flowchart.

0 / 16

// PSEUDOCODE · EXECUTION FLOW

// PSEUDOCODE · Binary Search
Find target in a sorted array in O(log n)
array = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] · target = 23
1function binarySearch(array, target):
2lo ← 0
3hi ← length(array) - 1
4while lo ≤ hi:
5mid ← lo + (hi - lo) / 2
6if array[mid] = target: return mid
7if array[mid] < target: lo ← mid + 1
8else: hi ← mid - 1
9return -1 // not found

Press PLAY to step through the algorithm line by line.

0 / 15

// TRY IT YOURSELF

Search any
sorted array.

Enter your own sorted array and a target. Watch the full step-by-step trace of lo, hi, and mid at each comparison. Try a target that isn't there to see how binary search proves absence efficiently.

// BINARY_SEARCH.exe

// LINEAR VS BINARY

Why binary search
is so fast.

Drag the slider to change the array size. See how linear search grows with N while binary search barely grows at all. This is the power of logarithmic time.

// LINEAR_VS_BINARY · ARRAY SIZE = 16
4N = 161024
LINEAR SEARCHO(n)
checks

Check every element one by one.

BINARY SEARCHO(log n)
checks

Halve the search space each step.

16 items → linear needs 16 checks, binary needs only 5. That's 3× faster!

// COMPARISONS NEEDED
Array size (n)Linear (n)Binary (⌈log₂ n⌉+1)Speedup
161653×
1,0001,0001191×
1,000,0001,000,0002147,619×
1,000,000,0001,000,000,0003132,258,065×

// PROPERTIES

4 things to
know about it.

Sorted input required

Binary search ONLY works on a sorted array. If the data is unsorted, you must sort it first — or use linear search instead.

O(log n) speed

Each comparison halves the search space. For 1 billion items, you need only ~30 checks. For 1 trillion, only ~40. It scales beautifully.

Divide & conquer

Binary search is the simplest example of the divide-and-conquer paradigm — the same idea behind merge sort and quicksort.

Foundation of CS

It underlies countless algorithms: balanced BSTs, exponentiation by squaring, finding roots, and optimization via binary search on the answer.

// DID_YOU_KNOW

Fun facts about
binary search.

01

First described in 1946

John Mauchly (co-creator of ENIAC) mentioned binary search in 1946, but a correct version with proper bounds wasn't published until 1962.

02

90% of programmers get it wrong

A famous 2006 study by Joshua Bloch found that nearly all implementations had bugs in edge cases — overflow, off-by-one, or infinite loops.

03

The mid-point overflow bug

Computing mid as (lo + hi) / 2 can overflow for huge arrays. The safe way is lo + (hi - lo) / 2, or the bit-shift lo + ((hi - lo) >> 1).

04

Used in real life constantly

Dictionary lookups, database indexes, git bisect, autocomplete, auto-tuning, finding square roots — binary search is everywhere.

05

You can binary search the answer

When the answer is a number in a range and you can test "is it achievable?", you can binary search the answer itself — a technique called "binary search on answer".

06

log₂(1,000,000) ≈ 20

A million sorted numbers can be searched in just 20 comparisons. A billion in 30. This is why logarithmic time is almost as good as instant.

// TUTORIAL QUIZZES · LEVELS 1–9

Test your mastery

Nine progressive quizzes from Level 1 to Level 9. Each has 10 questions with a 10-minute timer. XP scales with level — L1 gives 10 XP, L9 gives 90 XP. Click a quiz to expand and begin.

// REFERENCES

Sources & further reading

  1. [1]
    Introduction to Algorithms (CLRS)T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein — Chapter on divide & conquer
  2. [2]
    Programming PearlsJon Bentley — Column 2: binary search and algorithm correctness
  3. [3]
    The Art of Computer Programming, Vol. 3Donald Knuth — Section 6.2.1: searching an ordered table
  4. [4]
    AlgorithmsRobert Sedgewick & Kevin Wayne — binary search applications
  5. [5]
    Nearly All Binary Searches Are BrokenJoshua Bloch — Google Research Blog (famous off-by-one bug story)

// READY?

Master algorithms
with CodeTikki.

Practice 10,000+ coding problems, follow career roadmaps, and get hired.