Binary Search in AP CSA: Trace It Step by Step (2026)
Binary Search in AP CSA: Trace It Step by Step
Binary search is one of the most-tested algorithms on the AP CSA exam. On the 2026 exam, you will trace recursive binary search calls — not write the algorithm. This page teaches you exactly how to trace it, step by step, with the same patterns College Board uses.
📄 On This Page
🔍 How Binary Search Works
Binary search works by repeatedly dividing the search space in half. It requires a sorted array or ArrayList as a precondition. Here is the core logic:
- Calculate
mid = (left + right) / 2 - If the target equals the element at
mid, returnmid - If the target is less than the element at
mid, search the left half - If the target is greater, search the right half
- If
left > right, the target is not found — return-1
The exam always provides the full binary search method. Your job is to trace through the calls and determine the return value or how many times the method executes. Never memorize — always trace.
📋 Tracing Recursive Calls
The AP exam gives you a recursive implementation like this:
public static int bSearch(int[] arr, int left, int right, int x)
{
if (right >= left)
{
int mid = (left + right) / 2;
if (arr[mid] == x)
{
return mid;
}
else if (arr[mid] > x)
{
return bSearch(arr, left, mid - 1, x);
}
else
{
return bSearch(arr, mid + 1, right, x);
}
}
return -1;
}
Trace this with arr = {10, 20, 30, 40, 50} and x = 40:
| Call # | left | right | mid | arr[mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 2 | 30 | 30 < 40 → search right |
| 2 | 3 | 4 | 3 | 40 | 40 == 40 → return 3 |
Answer: 2 total calls, returns 3.
left > right. Count every call, including the one that returns -1. Students often miscount by one.
💻 Code Examples — Predict First
Before running each example, write down what you think the output will be. This is the single most effective exam study technique.
Example 1: Target Found Early
int[] vals = {0, 4, 4, 5, 6, 7};
int result = bSearch(vals, 0, vals.length - 1, 4);
System.out.println(result);
Answer: First call: left=0, right=5, mid=2. vals[2] is 4, which equals the target. Returns 2 on the first call. Note: there are two 4s in the array. Binary search found the one at index 2, but it could have found either — it does not guarantee finding the first occurrence.
Example 2: Target Not Found
int[] nums = {10, 20, 30, 40, 50};
int result = bSearch(nums, 0, nums.length - 1, 25);
System.out.println(result);
Trace:
| Call | left | right | mid | arr[mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 2 | 30 | 30 > 25 → search left |
| 2 | 0 | 1 | 0 | 10 | 10 < 25 → search right |
| 3 | 1 | 1 | 1 | 20 | 20 < 25 → search right |
| 4 | 2 | 1 | — | — | right < left → return -1 |
Answer: Returns -1. The method is called 4 times total.
Example 3: ArrayList Version
ArrayList data = new ArrayList<>();
// data contains: [0, 10, 30, 40, 50, 70, 70, 70, 70]
int result = binarySearch(data, 0, 8, 70);
Trace: Call 1: low=0, high=8, mid=4. data.get(4) is 50, which is less than 70. Call 2: low=5, high=8, mid=6. data.get(6) is 70. Returns 6. Note it found one of the 70s — not necessarily the first one.
❌ Common Pitfalls
Binary search ONLY works on sorted data. If the array is unsorted, the result is unpredictable. The exam may test this by asking which algorithms require sorted data.
When arr[mid] > target, search LEFT (smaller indices). When arr[mid] < target, search RIGHT (larger indices). Students frequently reverse this.
The initial call counts. If the method calls itself twice before returning, that is 3 total calls. Students often report only the recursive calls and forget the original.
Binary search returns AN index where the target exists, not necessarily the FIRST index. If duplicates exist, any matching index could be returned.
✏ Practice Questions
Select the best answer for each. Answers appear after you choose.
bSearch method shown above. What is the value of result after executing:int[] arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};int result = bSearch(arr, 0, 9, 23);
bSearch called (including the initial call) when searching for the value 23 in the array from Question 1?bSearch(arr, 0, 6, -5) searches for a value that is less than every element. How many times is bSearch called total?I. The data must be sorted in ascending order.
II. The data must contain no duplicate values.
III. The data must be stored in an array, not an ArrayList.
public static int search(int[] d, int lo, int hi, int t){ int m = (lo + hi) / 2; if (d[m] == t) return m; else if (d[m] > t) return search(d, lo, m, t); else return search(d, m, hi, t);}
[5, 15, 25, 35, 45, 55]. A binary search for the value 35 is performed. Which sequence of indices does mid take?{3, 1, 4, 1, 5, 9, 2, 6} to find the value 5. The method returns -1 (not found), even though 5 is in the array. What is the most likely cause?❓ Frequently Asked Questions
What is the precondition for binary search in AP CSA?
The array or ArrayList must be sorted in ascending order before binary search can be used. If the data is not sorted, binary search may return incorrect results or miss elements entirely.
Does the AP CSA exam use iterative or recursive binary search?
The 2026 AP CSA exam uses recursive binary search. Students must trace through recursive calls, tracking left, right, and mid values at each step. You will NOT be asked to write binary search from scratch.
How many comparisons does binary search need for an array of size n?
Binary search eliminates half the remaining elements with each comparison, giving it O(log n) efficiency. For an array of 1000 elements, it needs at most about 10 comparisons, compared to up to 1000 for linear search.
Can binary search be used on an ArrayList?
Yes. The AP CSA exam may show binary search operating on an ArrayList using .get(mid) instead of arr[mid]. The logic is identical, but the syntax uses ArrayList methods.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]