Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Binary search algorithm in AP Computer Science A

In AP Computer Science A, binary search is an algorithm for finding a value in a sorted array or ArrayList. It starts at the middle element and eliminates half of the remaining elements on each iteration or recursive call until it finds the target or runs out of elements.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is Binary search algorithm?

Binary search is the "guess the number between 1 and 100" strategy written as code. You don't guess 1, then 2, then 3. You guess 50, hear "higher" or "lower," and throw away half the possibilities in one move. Binary search does the same thing to a sorted array or ArrayList. It compares the target to the middle element. If they match, you're done. If the target is smaller, the search keeps only the left half. If the target is bigger, it keeps only the right half. Then it repeats on what's left.

The CED (EK 4.17.B.1) spells out the non-negotiable rule. The data must already be sorted. Without sorted order, "the target is bigger than the middle, so it must be on the right" stops being true, and the algorithm breaks. The search ends in one of two ways. Either it finds the value, or every element has been eliminated, which means the value isn't there. You can write binary search with a while loop (iteratively) or with a method that calls itself on a smaller range (recursively), per EK 4.17.B.3. In code, the search space is usually tracked with left and right indexes, and the middle comes from mid = (left + right) / 2 using integer division.

Why Binary search algorithm matters in AP® Computer Science A

Binary search lives in Unit 4: Data Collections, Topic 4.17 Recursive Searching and Sorting. It's the centerpiece of learning objective 4.17.B, which asks you to determine the result of each iteration of a binary search algorithm used to search for information in a collection. Notice the wording. The exam cares less about whether you can define binary search and more about whether you can trace it step by step and say which elements get checked.

It also ties into 4.17.A (determining the result of recursive algorithms on strings and collections), since the recursive version of binary search is a textbook example of recursion shrinking a problem. EK 4.17.B.2 adds the efficiency angle. Binary search is typically more efficient than linear search, which is why sorting data first is often worth it. Linear and binary are the only two search algorithms in scope for the course and exam, so knowing exactly how they differ covers the whole search portion of the topic.

How Binary search algorithm connects across the course

Linear Search (Unit 4)

Linear search is the brute-force cousin. It checks elements one at a time from the start and works on any array, sorted or not. Binary search trades that flexibility for speed. It needs sorted data, but it cuts the work in half with every check. For an array of about 1,000 elements, a binary search needs roughly 10 checks in the worst case, while a linear search might need all 1,000.

Merge Sort (Unit 4)

Merge sort and binary search sit side by side in Topic 4.17 for a reason. Binary search only works on sorted data, and merge sort is one way to get it sorted. Both also split a collection in half over and over. Merge sort splits to sort, then merges back up. Binary search splits to search and simply throws one half away.

Divide and Conquer (Unit 4)

Binary search is the simplest divide-and-conquer algorithm you'll see. It breaks one big problem (search 1,000 elements) into a smaller version of the same problem (search 500 elements). Once you see that pattern here, recursive algorithms like merge sort feel a lot less mysterious.

Recursion and Traversing Collections (Unit 4)

EK 4.17.A.1 says recursion can traverse Strings, arrays, and ArrayLists. Binary search shows what that looks like in practice. Each recursive call gets a narrower range of indexes, and the base case is either finding the target or having left pass right. Tracing the recursive version is the same skill as tracing the iterative one, just written as method calls instead of loop passes.

Is Binary search algorithm on the AP® Computer Science A exam?

Binary search shows up mainly in multiple-choice questions that make you trace the algorithm, matching learning objective 4.17.B. Expect these question types.

  • Trace the middle elements. You're given a sorted array, a target, and the formula mid = (left + right) / 2, and asked which elements get checked in order. For example, searching {1, 4, 7, 10, 13, 16, 19, 22, 25, 28} for 19 checks 13 (index 4), then 22 (index 7), then 16 (index 5), then 19 (index 6). Integer division truncates, so (5 + 6) / 2 is 5, not 5.5. That detail is where most traps hide.
  • Read the code. A method using left, right, and mid = left + (right - left) / 2 inside while (left <= right) asks you to predict a return value, count iterations, or spot what happens when the target is missing (often a return of -1).
  • Reason backward. Given a sequence of examined middle values, you decide what must be true about the array or the target's position.
  • Identify the algorithm. A stem describes "start at the middle element and eliminate half each iteration," and you pick binary search over linear search.

No released FRQ has used the term verbatim. Still, the tracing skill matters on free response too, since FRQs regularly ask you to loop through arrays and ArrayLists with careful index logic. Practice tracing on paper with a table that tracks left, right, and mid for every pass.

Binary search algorithm vs Linear Search

Linear search checks every element in order from the start until it finds the target, and it works on unsorted data. Binary search starts in the middle and eliminates half of the remaining elements each step, but it only works if the data is sorted. If a question says "start at index 0 and check each element," that's linear. If it says "start at the middle and discard half," that's binary. Binary search is typically faster, though not on every single input.

Key things to remember about Binary search algorithm

  • Binary search only works on an array or ArrayList that is already in sorted order.

  • Each step compares the target to the middle element and eliminates half of the remaining elements.

  • The search stops when the target is found or when every element has been eliminated, usually when left becomes greater than right.

  • The middle index is usually computed with integer division, so (left + right) / 2 rounds down, and tracing questions depend on that detail.

  • Binary search can be written iteratively with a while loop or recursively with a method that calls itself on a smaller range.

  • Binary search is typically more efficient than linear search, and these two are the only search algorithms tested on AP CSA.

Frequently asked questions about Binary search algorithm

What is binary search in AP Computer Science A?

Binary search is an algorithm that finds a value in a sorted array or ArrayList by checking the middle element and eliminating half of the remaining elements each step. It's covered in Topic 4.17 under learning objective 4.17.B, which asks you to trace each iteration.

Does binary search work on an unsorted array?

No. Per EK 4.17.B.1, the data must be sorted. On an unsorted array, the algorithm might throw away the half that actually holds the target and wrongly report that the value isn't there.

Is binary search always faster than linear search?

Not always. The CED says binary search is typically more efficient. If the target is the very first element, linear search finds it in one check while binary search starts in the middle. For large arrays and most targets, binary search wins by a lot.

What's the difference between binary search and linear search?

Linear search checks elements one by one from the start and works on any array. Binary search starts at the middle, cuts the search space in half each step, and requires sorted data. Those are the only two search algorithms in scope for the AP CSA exam.

Do I need to know recursive or iterative binary search for the AP exam?

Both. EK 4.17.B.3 says binary search can be written either way, so you might trace a while-loop version or a recursive method. The logic is the same, with left, right, and mid shrinking the range until the target is found or left passes right.