Searching: finding a value in a collection.
The AP CSA exam tests two algorithms: sequential search (works anywhere) and binary search (much faster, but only on sorted data).
Searching is the process of finding the index of a target value in an array or list. AP CSA tests two algorithms with very different requirements and performance characteristics.
Sequential search (also called linear search) walks the elements one at a time. It works on any data — sorted or not — but checks every element in the worst case.
Binary search repeatedly halves the search space. It is dramatically faster, but it only works on data that is already sorted.
On the AP exam, expect tracing questions, "how many comparisons?" questions, and FRQs that use search as a sub-step.
Linear walks vs halving searches.
Two algorithms, two trade-offs.
Sequential (linear) search
public static int sequentialSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
The standard "return -1 when not found" contract is universal across AP CSA. It signals failure clearly without using exceptions.
For Strings or other objects, use .equals() instead of ==.
Binary search
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
Each iteration eliminates half the remaining elements. The array must be sorted for the algorithm to be valid — otherwise the comparisons lead in the wrong direction.
Comparing performance
- Sequential search: up to
ncomparisons for an array ofnelements. - Binary search: about
log₂ ncomparisons.
For 1,000 elements, sequential may need up to 1,000 checks; binary needs at most about 10.
When to use which
- Unsorted data → sequential search.
- Sorted data → binary search.
- Cost of sorting first usually doesn't pay off for one-time small searches.
Searching vocabulary.
Each term is used precisely in AP MCQs and FRQs.
- Sequential / linear search — checks elements one at a time from the start.
- Binary search — repeatedly halves a sorted range.
- Target — the value being searched for.
- Mid index —
(low + high) / 2, the middle of the current range. - Comparison — one check between the target and an element.
- Best case / worst case — fewest / most comparisons possible.
- Sorted — elements arranged in non-decreasing order.
- Sentinel return value —
-1indicates "not found".
How searching is tested.
Tracing, comparison counts, and recognizing the sorted-data requirement.
MCQ patterns to expect:
- "How many comparisons for binary search?" Trace the
low,high,midvalues until you find the target or exit. - "Which array allows binary search?" Only the sorted one.
- "What does the method return when the target is missing?" Almost always
-1. - Best and worst case for sequential search. Best = 1 (target at index 0). Worst =
n(target absent or at the end).
FRQ tip: when implementing search, return the index immediately when found. Don't keep looping unnecessarily.
Trace a binary search and count comparisons.
Track low, high, and mid in a table.
Problem. Trace a binary search for the target 23 in the sorted array {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}. Indices are 0 through 9.
Step 1. low = 0, high = 9, mid = (0 + 9) / 2 = 4. arr[4] = 16. 16 < 23 → search right half. low = 5.
Step 2. low = 5, high = 9, mid = (5 + 9) / 2 = 7. arr[7] = 56. 56 > 23 → search left half. high = 6.
Step 3. low = 5, high = 6, mid = (5 + 6) / 2 = 5. arr[5] = 23. Match! Return 5.
Answer. Found at index 5 after 3 comparisons.
Compare to sequential. A linear search would need 6 comparisons (indices 0–5) to reach the same answer.
Why this matters. Many AP MCQs ask exactly this: "How many comparisons does binary search make for this array and target?" The answer is found by tracing low, high, and mid in a small table.
Search errors students keep making.
Most come from forgetting the "sorted" requirement.
- Using binary search on unsorted data. The result is meaningless.
- Forgetting
+ 1and- 1when shrinking the binary search range. Without them, the loop may not terminate. - Wrong loop condition. Use
while (low <= high). Using<misses the last comparison. - Returning the value instead of the index. The contract is to return the position.
- Using
==for Strings. Use.equals()in sequential search of String arrays. - Not returning
-1when the target is missing. Some students return0, which falsely suggests the value is at index 0.
Searching at a glance.
Pick the algorithm before writing code.
Searching Reference
AP Quick Reference| Algorithm | Requires sorted? | AP Exam Tip |
|---|---|---|
| Sequential search | No | Best 1, worst n comparisons. |
| Binary search | Yes | About log₂ n comparisons. |
| Not found | n/a | Both return -1 by convention. |
| String comparison | n/a | Use .equals(), not ==. |
| Binary search loop | n/a | while (low <= high) with mid = (low + high) / 2. |
| Range shrinking | n/a | low = mid + 1 or high = mid - 1. |
How to master searching.
Trace by hand, then code from memory.
Use the three practice tools below this lesson in order:
- MCQ Practice — for every binary search question, draw a table with columns
low,high,mid,arr[mid], action. Fill it in row by row. - Java Lab — implement both algorithms from scratch. Test sequential on unsorted data and binary on sorted data with targets that exist, are missing, are at the ends.
- FRQ Practice — when a problem requires finding the index of a value, choose sequential search by default unless the prompt promises sorted data. Always return
-1when not found.
If you can trace a binary search in under two minutes, you've mastered the most common search MCQ type.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.