← Learn AI
C_000042 · machine learning · intermediate

Binary Search

Locating an element in a sorted sequence by repeatedly halving the search interval, achieving logarithmic time.

Step 1 of 5

In words

What it is, why it matters, and what it is like.

Why am I learning this?

Binary search is the method used whenever you need to find one item in a massive list that has already been sorted, and you want the answer immediately. It is the engine behind how your phone finds a contact name in an instant, how a navigation app calculates the route from millions of roads, and how databases retrieve records without scanning every single file on the disk. Without this method, searching large datasets would require looking at almost every item one by one, which takes too long for any practical use. Mastering it gives you the ability to turn a task that feels like digging through a haystack into one that feels like opening a book to the right page.

The idea, in plain terms

Suppose you have a phone book with 1,000 pages, and you need to find the name 'Raman'. You could start on page 1 and flip forward one page at a time, checking every name. If you were unlucky, that would take 1,000 flips. But nobody actually does that. You open the book roughly in the middle, see that 'M' is there, and you know immediately that 'Raman' (which starts with 'R') cannot be anywhere in the first half — 'R' comes after 'M' alphabetically, so the first half of the book is entirely 'A'-to-'M' names. You throw away that half without looking at a single page of it. Now your book is half the size. You open the middle of the remaining half, see 'S', and know that 'Raman' must be before 'S', so the second half of what remains is useless. Half again. Each time you look at exactly one page, and you eliminate half of every page you did not look at. After a handful of checks, you are down to one page, and there is the name. That is binary search: you are not searching faster — you are searching *less* by eliminating half the remaining search space with every single comparison. The word 'binary' just means 'two choices' — each step asks: is what I want in the left half or the right half? And then you move to that half and ask again. The key trick works only because the data has an order: alphabetical order in the phone book, or numeric order in a sorted list of numbers. If the pages were in random order, opening to the middle would tell you nothing about where 'Raman' is — you could not eliminate anything. Order is what makes halving possible. That is why binary search requires sorted input: the order is what tells you which half to discard. With a million entries, the difference is stark. Checking one by one could take a million steps. Binary search needs at most about twenty comparisons — because 2 raised to the 20th power is already over a million, so twenty halvings reduce any search space of that size to a single item. Twenty checks versus a million. That is the whole point of the algorithm.

An analogy

Think of finding a word in a dictionary. You do not start at 'A' and read every entry. You open roughly halfway, see the word 'M'. Your word begins with 'P', so you know the first half of the dictionary — everything from 'A' through 'M' — is useless. You do not glance at it again. You now have a dictionary that is half the size. Open that in the middle, see 'R'. Your 'P' is before 'R', so the entire back half of what remains is thrown away. Now you are holding a dictionary a quarter of the original size. Open the middle again, see 'O'. 'P' is after 'O', so the front half of this quarter goes. Each glance at one page eliminates half of the remaining book. After a handful of glances, the book is reduced to a single page carrying your word. This analogy works beautifully because the mechanism is identical: each comparison halves the search space, and the order of the entries is what makes the halving valid. The analogy stops working in one particular way: the dictionary looks up words, which have a natural order (alphabetical), but binary search works on anything with a comparable order — numbers, timestamps, or even a list of names sorted by last name.

Definition

Binary search is a method for finding a specific item in a sorted list by repeatedly checking the middle item and discarding the half of the list that cannot contain the target, until only the target remains.

Where this sits

This concept sits directly beside Linear Search, which is the naive approach of checking items one by one from start to finish, and Binary Trees, which are a data structure designed to keep items sorted so you can jump between them like pages in a book. While Linear Search tells you how slow it is to do nothing but look at every option, Binary Trees show you how to organize your data physically or digitally so that the halving strategy works efficiently without jumping back and forth across a flat list.

Signal from the Frontier

Get the next essay on mind, machine, and meaning

Essays at the intersection of AI, philosophy, and Indian governance. No promotional content.

We'll send a one-click sign-in link to confirm. No password needed.

Views expressed are personal and do not represent the Government of India or the Government of Uttarakhand.