---
title: "Binary Search | FiveStudy"
description: "How binary search halves a sorted list with each look, how to count the looks it needs, and why it fails on unsorted data."
url: https://fivestudy.com/guides/ap-csp-3-11-binary-search
---

[AP Computer Science Principles](https://fivestudy.com/ap-computer-science-principles) · Study guide

# Binary Search

[Figure: FiveStudy]

FiveStudy

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

# Binary Search

How binary search finds a value in a sorted list by looking at the middle and throwing away half each time, how to count its looks, and why the list has to be sorted.

1. The idea 2. Looking for 37 3. When the value is not there 4. Counting looks 5. Why the list must be sorted 6. Binary or linear? 7. Worked examples 8. Common mistakes

## 1 The idea

Think of the number guessing game: “I'm thinking of a number from 1 to 100.” The smart first guess is 50. If the answer is “higher,” you never need to think about 1 to 50 again. Each guess cuts the possibilities in half.

**Binary search** does this with a list. It starts at the **middle of a sorted data set**, compares that element with the value it wants, and **eliminates half** of the data. It repeats on the half that is left until it **finds the value or has eliminated every element**.

## 2 Looking for 37

The list below is sorted from smallest to largest. When the part being searched has an even number of elements there is no single middle, so these examples use the one just before the center (the middle index rounded down).

**Figure 1.** Each look eliminates about half of what is left. Three looks find 37; a linear search would have checked 10 elements.

| Look | Part still searched (indexes) | Middle index | Middle value | Decision |
| --- | --- | --- | --- | --- |
| 1 | 1 to 15 | 8 | 30 | 37 is larger: drop the left part |
| 2 | 9 to 15 | 12 | 48 | 37 is smaller: drop the right part |
| 3 | 9 to 11 | 10 | 37 | found at index 10 |

Three looks. The decision at each step depends on one fact: in a sorted list, everything to the left of the middle is smaller and everything to the right is larger. That is why one comparison can rule out half the list at once.

## 3 When the value is not there

Search the same list for 40:

| Look | Part still searched | Middle value | Decision |
| --- | --- | --- | --- |
| 1 | 1 to 15 | 30 | 40 is larger: drop the left part |
| 2 | 9 to 15 | 48 | 40 is smaller: drop the right part |
| 3 | 9 to 11 | 37 | 40 is larger: drop the left part |
| 4 | 11 to 11 | 41 | 40 is smaller: drop the right part |

After look 4 nothing is left to search: 40 would have to sit between 37 and 41, and there is no element there. The search reports that 40 is not in the list after 4 looks, without checking the other 11 elements.

## 4 Counting looks

Each look halves the part that is left, so the most looks a search can need is the number of times you can halve the list size before reaching 1, plus one for the last look. Doubling the list adds only **one** more look.

| Elements in the sorted list | Binary search: most looks | Linear search: most looks |
| --- | --- | --- |
| 15 | 4 | 15 |
| 100 | 7 | 100 |
| 1,000 | 10 | 1,000 |
| 1,000,000 | 20 | 1,000,000 |

To estimate by hand, halve and count: 100 → 50 → 25 → 12 → 6 → 3 → 1 is 6 halvings, so at most 7 looks. For the lists of 15, 100, and 1,000 elements, a program confirmed the binary search numbers by searching for every value in the list and for every missing value in between; the million-element row uses the same halving count.

When a question gives you the list and the target, don't estimate: simulate the search look by look, as in the tables above, and count every look including the one that finds the value.

## 5 Why the list must be sorted

**Data must be in sorted order to use binary search.** The rule “the target is bigger than the middle, so it must be to the right” is only true when the list is in order. Here is what happens on an unsorted list, searching for 8:

**Figure 2.** On unsorted data the “smaller, so look left” reasoning is false. The search does not crash or warn; it gives a wrong answer.

The search reasons “15 is bigger than 8, so 8 must be to the left,” and it is right by luck. Then “33 is bigger, so 8 must be to the left of 33”: wrong, because 8 is to its right. The run:

| Look | Part still searched | Middle value | Decision |
| --- | --- | --- | --- |
| 1 | 1 to 7 | 15 | 8 is smaller: keep the left part |
| 2 | 1 to 3 | 33 | 8 is smaller: keep the left part |
| 3 | 1 to 1 | 40 | 8 is smaller: keep the left part |

Binary search on unsorted data does not crash and does not warn you. It reports “not found” for a value that is right there. If the data is not sorted, sort it first or use a linear search.

## 6 Binary or linear?

On sorted data, binary search is often much more efficient than linear search: finding 37 took 3 looks instead of 10, and for a million elements the difference is 20 looks against a million. Linear search still has its place:

| Situation | Better choice | Why |
| --- | --- | --- |
| Large list, already sorted, searched often | Binary search | Few looks each time |
| Unsorted list, searched once | Linear search | Binary search would need the list sorted first, which takes longer than one linear search |
| Very short list | Either | With 5 elements the difference is a look or two |
| Value is first in the list | Linear search | It finds it on the first check |

The key idea

Binary search starts at the middle of a sorted list and eliminates half of what is left with each look, until the value is found or nothing is left. It needs sorted data, and on sorted data it is often far faster than linear search: doubling the list adds only one look.

## 7 Worked examples

Worked Example 1 Count the looks

### Two targets, one list

Binary search is used on the sorted list 4, 9, 13, 17, 22, 28, 31, 36, 40, 45, 52 (middle rounded down). How many elements are examined to find 45? To find 4?

1. 1

   45: look 1 checks 28; look 2 checks 40; look 3 checks 45.

2. 2

   4: look 1 checks 28; look 2 checks 13; look 3 checks 4.

3. 3

   A linear search would need 10 checks for 45 but only 1 for 4.

**Answer:** 3 looks for 45 and 3 looks for 4.

Worked Example 2 Worst case

### 500 sorted values

What is the greatest number of looks a binary search can need on a sorted list of 500 elements?

1. 1

   Halve and count: 500 → 250 → 125 → 62 → 31 → 15 → 7 → 3 → 1.

2. 2

   That is 8 halvings, plus the final look.

**Answer:** 9 looks. Linear search could need all 500.

Worked Example 3 Check the requirement

### The class roster

A teacher's attendance list is in the order students arrived. Can a binary search find a student's name in it? What should the teacher do?

1. 1

   Binary search needs the data in sorted order, here alphabetical order.

2. 2

   Arrival order is not sorted, so the halving reasoning fails and the search can miss names that are there.

**Answer:** Not reliably. Sort the list alphabetically first, or use a linear search.

Worked Example 4 Estimate

### A school directory

A district directory has 30,000 names in alphabetical order. About how many looks does a binary search need at most? A linear search?

1. 1

   2^14 = 16,384 is less than 30,000, and 2^15 = 32,768 is more.

2. 2

   So the list can be halved 14 times before reaching 1, and one more look finishes: 15.

**Answer:** At most 15 looks for binary search, compared with up to 30,000 for linear search.

## 8 Common mistakes

Common Mistake 1 · Taking the middle of the whole list every time

After the first look, the middle is the middle of the part still being searched, not of the original list.

Common Mistake 2 · Not counting the look that finds it

Finding 37 took three looks: 30, 48, and 37. The final comparison counts.

Common Mistake 3 · Using binary search on unsorted data

It gives wrong answers without any error. The sorted requirement is not optional.

Common Mistake 4 · Thinking binary search is always better

On an unsorted list that will be searched once, sorting first costs more than one linear search.

Explain binary search like this

“Binary search compares the target with the middle element of the sorted list. If it is not a match, the comparison shows which half the target must be in, so the other half is eliminated. This repeats on the remaining half until the target is found or no elements are left. It requires the list to be sorted.”

Name the middle, the elimination of half, how it ends, and the sorted requirement.

FiveStudy AP® Computer Science Principles · Binary Search
