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.

⏰ The AP CSA Exam is May 15, 2026 — Get the 4-Week Cram Kit →

🔍 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:

  1. Calculate mid = (left + right) / 2
  2. If the target equals the element at mid, return mid
  3. If the target is less than the element at mid, search the left half
  4. If the target is greater, search the right half
  5. If left > right, the target is not found — return -1
🎓 AP Exam Tip

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.

⚠ Watch Out! When the target is NOT in the array, trace until 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

✎ Predict: What index is returned? How many calls?
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

✎ Predict: What is returned? How many recursive calls?
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

✎ Predict: What value is returned by the call?
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

Pitfall 1: Forgetting the Sorted Precondition

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.

Pitfall 2: Wrong Mid Calculation Direction

When arr[mid] > target, search LEFT (smaller indices). When arr[mid] < target, search RIGHT (larger indices). Students frequently reverse this.

Pitfall 3: Miscounting Recursive Calls

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.

Pitfall 4: Assuming First Occurrence Is Found

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.

Score: 0 / 8
1. Consider the recursive 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);
2. How many times is bSearch called (including the initial call) when searching for the value 23 in the array from Question 1?
3. A sorted array of length 7 contains only positive integers. The call bSearch(arr, 0, 6, -5) searches for a value that is less than every element. How many times is bSearch called total?
4. Which of the following is a necessary precondition for binary search to work correctly?
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.
5. The following code contains an error that causes infinite recursion for some inputs. Which line is the problem?
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);
}
6. An ArrayList contains [5, 15, 25, 35, 45, 55]. A binary search for the value 35 is performed. Which sequence of indices does mid take?
7. What is the maximum number of comparisons binary search needs for a sorted array of 128 elements?
8. A programmer uses binary search on the array {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.

About the Author: Tanner Crow has 11+ years of AP Computer Science teaching experience, 1,845+ verified tutoring hours, and a 5.0 rating. His students score 5s at over double the national rate. Book a tutoring session →

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]