Linear search checks a list's elements one at a time from the start, so it works on any list but may check every element. Binary search checks the middle of a sorted list and discards the half that cannot hold the target, so it needs sorted data but checks far fewer elements. In AP® Computer Science Principles you compare them by counting checks: doubling a list doubles linear search's worst case but adds one to binary search's.
Quick check
How many elements does FOR EACH visit in the list [8, 3, 5]?Show answerHide answer
3, one pass for each element.
Is the list [2, 9, 9, 14] sorted?Show answerHide answer
Yes. Each value is at least as large as the one before it, so it is in ascending order; repeats are allowed.
You guess a number from 1 to 100 and hear "higher" or "lower" each time. Which first guess rules out the most?Show answerHide answer
A guess in the middle, such as 50. Whatever the reply, about half the numbers are ruled out, which is the idea behind binary search.
Know these already? Jump to the practice questions
What you'll be able to do
- Calculate how many elements a linear search checks for a given list and target.
- Use low, high and middle indexes to trace a binary search on a sorted list.
- Explain why binary search needs sorted data.
- Compare how each search's worst-case number of checks grows as a list doubles.
Key terms
Linear search
A search that checks each element in order from the first until it finds the target or runs out. Also called sequential search.
- In plain English
- Look in every box, starting at the front.
- Don't write
- "Linear search needs the list to be sorted."
Binary search
A search on a sorted list that checks the middle element and discards the half that cannot hold the target, repeating until it is found or no elements remain.
- In plain English
- Keep halving the list until you find it.
- Don't write
- "Binary search works on any list."
Worst case
The input of a given size that makes an algorithm do the most work.
- In plain English
- The unluckiest search.
- Don't write
- "The worst case is when the program crashes."
How does linear search work?
Start at index 1, compare each element with the target, and stop at the first match. This version leaves pos at 0 if the target is missing.
nums ← [14, 3, 9, 21, 6, 9]
target ← 9
pos ← 0
i ← 1
REPEAT UNTIL(pos ≠ 0 OR i > LENGTH(nums))
{
IF(nums[i] = target)
{
pos ← i
}
i ← i + 1
}
Count one check per element compared. If the target first appears at index k, the search makes k checks. If it is missing, every element is checked, so a list of n elements takes n checks: that is the worst case.
Linear search never relies on order, so it works on unsorted lists. Both searches are Learn this: the exam reference sheet gives only the pseudocode.
Check yourself
Show answerHide answer
- It stops at the first 8, at index 2.
How does binary search work?
Binary search keeps a range of indexes, from low to high, that could still hold the target.
- Set
lowto 1 andhighto the length of the list. - Check the middle index: (
low+high) / 2, rounded down when it is not a whole number. - If the middle element is the target, stop. If it is smaller than the target, set
lowto the middle index + 1. If it is larger, sethighto the middle index − 1. - If
lowpasseshigh, the target is not in the list. Otherwise go back to step 2.
This note rounds the middle down. Some versions round up, which changes which elements are checked but not the worst-case count.
Key idea
Each binary search check halves the range that could still hold the target.
Check yourself
Show answerHide answer
Index 6: (4 + 9) / 2 is 6.5, which rounds down to 6.
Why does binary search need sorted data?
Discarding half is safe only if the order guarantees the target can't be there. In an ascending list, everything left of a smaller middle element is smaller still. In an unsorted list that guarantee is gone, and binary search can discard the half that holds the target.
Sorting a list first means looking at every element at least once, which is already as much work as a linear search's worst case. So for one search of an unsorted list, use linear search; binary search pays off when sorted data is searched many times.
Check yourself
Show answerHide answer
7 is smaller than 9, so it goes right, to [4, 5], and never finds the 9 at index 1.
How do the counts grow as the list doubles?
For binary search's worst case, halve the list size again and again (rounding down) until you reach 1, and count the numbers you wrote. For 1,000: 1000, 500, 250, 125, 62, 31, 15, 7, 3, 1, which is 10 checks.
| List size | Linear search, worst case | Binary search, worst case |
|---|---|---|
| 8 | 8 | 4 |
| 16 | 16 | 5 |
| 1,000 | 1,000 | 10 |
| 2,000 | 2,000 | 11 |
| 1,000,000 | 1,000,000 | 20 |
Doubling the list adds one binary search check, because one extra check halves the doubled list back to the old size. The note on algorithmic efficiency names these kinds of growth.
Check yourself
Show answerHide answer
- Doubling the list adds one check.
Worked example 1: Count linear search checks
Question: Linear search runs on nums ← [14, 3, 9, 21, 6, 9]. How many elements does it check when the target is (a) 9 and (b) 5?
What tells you the method
Linear search checks: count from index 1 to the first match.
Step 1: Target 9.
It checks 14, 3, then 9 at index 3 and stops.
WhyLinear search stops at the first match, so the second 9 is never reached.
Step 2: Target 5.
No element is 5, so it checks all 6.
WhyA missing target is the worst case.
Check it
In the pseudocode, pos becomes 3 for target 9 after 3 passes, and stays 0 for target 5 after 6 passes.
Answer
(a) 3 (b) 6
Worked example 2: Trace a binary search
Question: Binary search, rounding the middle down, looks for 41 in this sorted list. How many elements does it check?
[2, 5, 8, 12, 16, 23, 38, 41, 56, 72, 91]
What tells you the method
A binary search count: track low, high and the middle index in a table.
Step 1: Trace the range.
lowhighMiddle Element Decision 1 11 6 23 23 < 41: low← 77 11 9 56 56 > 41: high← 87 8 7 38 38 < 41: low← 88 8 8 41 found Why(7 + 8) / 2 is 7.5, which rounds down to 7.
Step 2: Count the rows.
Each row is one element checked, so there are 4.
WhyEvery pass compares exactly one middle element with the target.
Check it
The list has 11 elements; halving gives 11, 5, 2, 1, so the worst case is 4 checks, and this search did not exceed it.
Answer
4
Worked example 3: Predict the effect of doubling
Question: A sorted list of 1,000 elements grows to 2,000. Which statement describes the worst-case number of elements each search checks?
- A) Linear: 1,000 to 1,001. Binary: 10 to 20.
- B) Both searches check twice as many.
- C) Linear: 1,000 to 2,000. Binary: 10 to 11.
- D) Linear: 1,000 to 2,000. Binary: stays at 10.
What tells you the method
"As the list doubles" means work out each worst case before and after.
Step 1: Linear search.
Its worst case checks every element: 1,000, then 2,000.
WhyA missing target is checked against every element.
Step 2: Binary search.
Halving 1,000 gives 10 numbers; halving 2,000 gives 2000, 1000, …, 1, which is 11.
WhyEach doubling adds one number to the halving list.
Check it
After one check on 2,000 elements, at most 1,000 remain, and those need at most 10 more: 11 in all.
Answer
C
Your turn
Question: Binary search, rounding the middle down, looks for 50 in this sorted list. How many elements does it check before it reports that 50 is missing?
[3, 7, 10, 14, 19, 22, 25, 31, 36, 40, 44, 52, 58, 63, 70]
Step 1: First check.
lowis 1 andhighis 15, so the middle is 8. 31 < 50, solowbecomes 9.Step 2: Your go: keep tracing until
lowpasseshigh, then count the checks.
Show the full solutionHide the solution
Step 2. Middle (9 + 15) / 2 = 12: 52 > 50, so high is 11. Middle (9 + 11) / 2 = 10: 40 < 50, so low is 11. Middle 11: 44 < 50, so low is 12, which passes high (11), so the search stops. Checked: 31, 52, 40, 44.
Answer: 4
Traps
Trap
Binary search works on any list.
It seems right because it only compares values. But discarding half is safe only when the list is sorted; on unsorted data it can throw away the half that holds the target.
Can binary search on [9, 2, 7, 4, 5] be trusted to find 9?
No. The list is unsorted; this search goes right after checking 7 and misses the 9.
Trap
Doubling the list doubles binary search's checks too.
It seems right because that is what happens to linear search. But one extra check halves the doubled list back to the old size, so the worst case rises by 1.
At most how many binary search checks does a sorted list of 2,000 need, if 1,000 needs 10?
What's examined
On the AP® Computer Science Principles exam, searching belongs to Big Idea 3: Algorithms and Programming. Multiple-choice questions ask which search suits a situation, why binary search needs sorted data, and how many elements a search checks for a given list or list size.
Practice questions
- Recall Which condition must a list meet before binary search can be used on it?
- A) It must have an even number of elements.
- B) It must be sorted.
- C) It must contain no repeated values.
- D) It must contain the target.
Show answer
Answer: B. Discarding half the list is safe only when the elements are in order.
Why the others are wrong:
- A confuses the middle index with an exact half; rounding handles odd and even lengths.
- C is not required: repeats keep a list sorted.
- D is not required: binary search reports a missing target when
lowpasseshigh.
- Apply Linear search runs on
[8, 15, 4, 23, 16, 42, 4]. How many elements does it check when the target is (a) 42 and (b) 10?
Show answer
Answer: (a) 6 (b) 7
Marking points:
- 42 is at index 6, so 6 elements are checked.
- 10 is missing, so all 7 elements are checked.
- Apply Binary search, rounding the middle down, looks for 13 in
[4, 9, 13, 18, 27, 31, 35, 48, 52]. (a) Which element is checked second? (b) How many elements are checked in total?
Show answer
Answer: (a) 9 (b) 3
Marking points:
- Middle (1 + 9) / 2 = 5: 27 > 13, so
highis 4. - Middle (1 + 4) / 2 = 2.5, rounded down to 2: 9 < 13, so
lowis 3. - Middle (3 + 4) / 2 = 3.5, rounded down to 3: 13 is found. Checks: 27, 9, 13.
- Exam-style A list holds the whole numbers 1 to 15 in order. For which target does linear search check fewer elements than binary search (middle rounded down)?
- A) 15
- B) 8
- C) 12
- D) 1
Show answer
Answer: D. Linear search finds 1 on its first check; binary search checks 8, 4, 2 and then 1, which is 4 checks.
Why the others are wrong:
- A takes linear search 15 checks and binary search 4.
- B is the first middle, so binary search needs only 1 check against linear's 8.
- C takes binary search 2 checks (8, then 12) against linear's 12.
- Exam-style What is the greatest number of elements binary search checks in a sorted list of (a) 100 elements and (b) 200 elements?
Show answer
Answer: (a) 7 (b) 8
Marking points:
- Halving 100 gives 100, 50, 25, 12, 6, 3, 1: 7 numbers.
- Doubling to 200 adds one more: 8.
- Recall Name the search that works whether or not the list is sorted.
Show answer
Answer: Linear search.
Marking points:
- It checks every element in turn, so it never relies on their order.
- Exam-style On which list is binary search guaranteed to find any target that is in the list?
- A) [5, 12, 40, 61, 88]
- B) [5, 40, 12, 88, 61]
- C) [61, 88, 5, 12, 40]
- D) [12, 5, 40, 88, 61]
Show answer
Answer: A. It is the only list in ascending order.
Why the others are wrong:
- B is out of order: a search for 40 checks 12, goes right and misses it.
- C is out of order: a search for 61 checks 5, goes right and misses it.
- D is out of order: a search for 5 checks 40, then 12, and misses it.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- Linear search works on any list; its worst case checks all n elements.
- Binary search needs sorted data and halves the remaining range with every check.
- For binary search's worst case, halve the size down to 1 and count the steps.
- Doubling a list doubles linear search's worst case but adds one binary search check.
- For a single search of unsorted data, linear search beats sorting first.