Introduction

Let’s understand binary search step by step, with an example you can trace on paper. Imagine you’re looking for the word “python” in a dictionary. You don’t start at page 1 and read every word β€” you open the middle, check where you landed, and throw away half the book in one move. That’s binary search, and honestly, once it clicks, you’ll wonder why you ever searched any other way.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

What Is Binary Search?

Binary search is a fast way to find something in a sorted list. Instead of checking items one by one, it looks at the middle item, decides whether your target is to the left or right, and throws away the wrong half β€” again and again until the target is found.

The main idea

Think of the guessing game where I think of a number between 1 and 100 and you guess. If you guess 50 and I say "higher", you've just eliminated 50 numbers in one move. Binary search plays that game with data β€” every guess cuts the remaining work in half. That halving is why it stays fast even when a list has a million items.

In simple words

  1. Look at the middle item of the sorted list.
  2. If it's your target β€” done! If it's smaller than your target, throw away the left half.
  3. If it's bigger than your target, throw away the right half.
  4. Repeat on the half that's left until you find it (or run out of items).

One condition is non-negotiable: the list must be sorted first. Binary search on an unsorted list is like using a dictionary with shuffled pages β€” useless. If you need a refresher on why sorted data matters, my data structures guide explains how lists and arrays are organized.

Let's Solve an Example: Find 23

Here's our sorted list of 10 numbers, and we want to find 23:

[2, 5, 8, 12, 16, 23, 38, 56, 72, 91]

Guess 1 β€” check the middle: The middle index is 4, which holds 16. 16 is smaller than 23, so the answer must be on the right. We throw away [2, 5, 8, 12, 16] β€” half the list gone in one move.

Guess 2 β€” middle of what's left: We now look at [23, 38, 56, 72, 91]. Its middle is 56. 56 is bigger than 23, so we throw away the right side β€” [56, 72, 91] gone.

Guess 3 β€” middle again: Left with [23, 38], the middle is 23. Found it β€” in just 3 guesses. Checking one by one would have taken 6. Most beginners get stuck here because they try to memorize the steps; don't. Just remember: middle, compare, throw away half.

Here's the whole search as a picture, plus a short animation walking through it:

Binary search worked example: finding 23 in a sorted list
Animated binary search finding 23 step by step

Binary Search in Python

Here's the whole idea in about 10 lines of code β€” this is the version I recommend typing out yourself, not copying:

def binary_search(numbers, target):
    low, high = 0, len(numbers) - 1
    while low <= high:
        mid = (low + high) // 2
        if numbers[mid] == target:
            return mid          # found it!
        elif numbers[mid] < target:
            low = mid + 1       # search the right half
        else:
            high = mid - 1      # search the left half
    return -1                   # not in the list

Run it on our example and it returns index 5 β€” exactly where 23 sits. Try changing the target to 7 and watch it return -1. That low <= high condition is the quiet hero: it's what stops the loop when the list runs out.

Binary Search vs Linear Search: Which One?

Linear search checks items one by one from the start β€” simple, but slow on big lists. Here's the honest comparison:

Binary searchLinear search
Needs sorted data?Yes β€” alwaysNo
Guesses for 1,000,000 itemsAbout 20Up to 1,000,000
Best forBig sorted listsSmall or unsorted lists
Code complexityA little trickierDead simple

That "about 20" is the real magic β€” it's the same reason Big-O notation calls binary search O(log n). In my opinion, every beginner should learn linear search first and then binary search β€” because the contrast is what makes the speed feel real. And if you're learning Python, Python lists and loops are the perfect warm-up before writing search code.

Test Your Understanding

Question: In a sorted list of 1,000 items, roughly how many guesses does binary search need at most?

  1. About 10
  2. About 500
  3. About 1,000
  4. Exactly 1

Answer: A β€” each guess halves the list, so 1,000 items take about 10 guesses (2^10 = 1,024). That's the power of halving.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

Related Articles

Conclusion

Binary search finds things fast by checking the middle, throwing away the wrong half, and repeating β€” 3 guesses instead of 6 for our little list, and about 20 instead of a million for a huge one. Your homework: write the Python version above, run it on your own sorted list, and count the guesses. The moment you watch a million-item list surrender in 20 steps, you'll never search the slow way again.

Never miss a new article

Subscribe to The Geeks Guru newsletter and get every new article in your inbox.

Loading