Keep a sorted interval for binary search
Quick review (2 minutes)
Someone thinks of a whole number from a known range and answers each guess with higher, lower or correct.
Binary search works only on a sorted row. Each check compares the target with the middle card of the interval: a match ends the search, and otherwise the interval keeps only the half that can still hold the target. When the interval is empty, the target is not in the row.
Check that the row is sorted, smallest first. If it is not, binary search cannot be used.
This lesson numbers positions from 1: write low = 1 and high = the number of cards, so the interval starts as the whole sorted row.
If low is greater than high, the interval is empty: announce not found and stop.
Work out middle = (low + high) / 2, rounded down: (1 + 2) / 2 is 1.5, which rounds down to 1. Read the card at that position. The search starts at the middle of the interval.
Compare the target with that card and count 1 check: on a match, announce found at position middle and stop. For a smaller target write high = middle - 1, for a larger target write low = middle + 1, and go back to the third step.
Binary search on a row that is not sorted can miss a target that is there. In the row 9 2 7 4 5, a search for 4 reads the middle card 7, keeps positions 1 to 2 because 4 is smaller, reads the 9, and ends with not found, though the 4 sits at position 4.
1 In plain words
Someone thinks of a whole number from a known range and answers each guess with higher, lower or correct. A guess in the middle of the numbers still possible either ends the game or rules out about half of them.
Binary search finds a target in a row the same way. It starts at the middle of a sorted row, and each comparison that misses rules out at least half of the cards that are left, until the target is found or no card is left. The row must be in sorted order, smallest first in this unit.
The cards still in play form the interval: every card from a low position to a high position. The binary search of this lesson has five steps. Step 1: set low to 1 and high to the number of cards. Step 2: if low is greater than high, announce not found and stop. Step 3: set middle to the position halfway between low and high, (low + high) / 2, rounded down when it is not a whole number. Step 4: compare the target with the card at middle. If it matches, announce found and stop. If the target is smaller, set high to middle - 1. If the target is larger, set low to middle + 1. Step 5: go to step 2. These steps are this lesson's way of writing the search the Field Guide and the AP course describe.
Remember one sentence: binary search keeps only the half that can still hold the target. To look it up, see essential knowledge AAP-2.P.1 and AAP-2.P.2 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of a binary search for 23 in the sorted row 2 5 8 12 16 23 38 41 56 72 81 85 90 94 99. Check 1 reads 41, the middle card of positions 1 to 15. Check 2 reads 12, the middle card of positions 1 to 7. Check 3 reads 23, the target, the middle card of positions 5 to 7. The last row is marked.
3 Why
Sorting is what lets the search drop a half: the dropped positions cannot hold the target. In a row sorted smallest first, every card to the right of the middle is at least as large as the middle card. If the target is smaller than the middle card, it cannot be to the right, so high moves to middle - 1. If the target is larger, it cannot be to the left, so low moves to middle + 1.
A check that misses removes the middle card and the half that cannot hold the target; a match ends the search. In the sorted row of 15 cards used here, the interval goes from 15 cards to at most 7, then 3, then 1, so any search ends after at most 4 checks. The Field Guide gives the same kind of count: at most 10 checks for 1,000 boxes.
On data that is not sorted, a miss still drops at least half of the interval, the middle card among them, and the dropped cards can include the target. Binary search is often more efficient than linear search on sorted data, and it cannot be used on data that is not sorted.
4 The rule
Binary search works only on a sorted row.
Each check compares the target with the middle card of the interval: a match ends the search, and otherwise the interval keeps only the half that can still hold the target. When the interval is empty, the target is not in the row.
5 How
- Check that the row is sorted, smallest first. If it is not, binary search cannot be used.
- This lesson numbers positions from 1: write low = 1 and high = the number of cards, so the interval starts as the whole sorted row.
- If low is greater than high, the interval is empty: announce not found and stop.
- Work out middle = (low + high) / 2, rounded down: (1 + 2) / 2 is 1.5, which rounds down to 1. Read the card at that position. The search starts at the middle of the interval.
- Compare the target with that card and count 1 check: on a match, announce found at position middle and stop. For a smaller target write high = middle - 1, for a larger target write low = middle + 1, and go back to the third step.
6 Trace
| Check | Interval | Middle card | Then | Explanation |
|---|---|---|---|---|
| 1 | 1 to 15 | 41 at position 8 | 50 > 41: low = 9 | (1 + 15) / 2 is 8. The target is larger, so the left half and the middle go. |
| 2 | 9 to 15 | 85 at position 12 | 50 < 85: high = 11 | (9 + 15) / 2 is 12. The target is smaller, so the right half and the middle go. |
| 3 | 9 to 11 | 72 at position 10 | 50 < 72: high = 9 | (9 + 11) / 2 is 10. The target is smaller again. |
| 4 | 9 to 9 | 56 at position 9 | 50 < 56: high = 8 | One card is left, and it is not the target. |
| — | 9 to 8 | — | not found | Low, 9, is greater than high, 8: the interval is empty, so 50 is not in the row. |
7 The trap
Binary search on a row that is not sorted can miss a target that is there. In the row 9 2 7 4 5, a search for 4 reads the middle card 7, keeps positions 1 to 2 because 4 is smaller, reads the 9, and ends with not found, though the 4 sits at position 4.
Quick checks
Quick check 1 of 2
A binary search looks for 90 in the sorted row 2 5 8 12 16 23 38 41 56 72 81 85 90 94 99. Which middle cards does it read, and how many checks does it make?
Show the steps and the answer
It reads 41, 85, 94 and then 90, so it makes 4 checks and finds 90 at position 13.
Why: Positions 1 to 15 give middle 8, the 41, and 90 is larger, so low = 9. Positions 9 to 15 give middle 12, the 85, so low = 13. Positions 13 to 15 give middle 14, the 94, and 90 is smaller, so high = 13. Position 13 holds 90.
Quick check 2 of 2
The row 9 2 7 4 5 is not sorted. A binary search for 4 follows the steps anyway. Which cards does it read, and what does it announce?
Show the steps and the answer
It reads 7 and then 9, and it announces not found, though 4 is at position 4.
Why: Middle 3 holds 7, and 4 is smaller, so high = 2. Middle 1 holds 9, and 4 is smaller, so high = 0, below low. The 4 was in the dropped positions 4 to 5: in a row that is not sorted, a card smaller than the middle card can lie to its right.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- Computer Science Field Guide: Algorithms, 2.2 Searching. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 2 Dec 2026.
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 2 Dec 2026.