Everything you need to know — algorithms, complexity, code, and exam strategy.
The AP exam tests two searching algorithms applied to arrays and ArrayLists:
Three sorting algorithms are explicitly required:
Check each element one by one from left to right until you find the target or exhaust the array. No sorting required.
Repeatedly halve the search space. Compare the target to the middle element. If target is smaller, search the left half; if larger, search the right half. Repeat until found or bounds cross.
Key variables to track on the exam: low, high, mid. Examiners will ask you to trace these values.
Find the minimum element in the unsorted portion, swap it with the first unsorted element. Repeat, extending the sorted portion by one each pass.
Mental model: "Find the smallest, put it in place. Find the next smallest, put it in place. Repeat."
Trace example — array: [5, 2, 8, 1, 9]
Take each element and insert it into its correct position within the already-sorted left portion. Shift elements right to make room.
Mental model: Like sorting playing cards in your hand — pick up each card and slide it left to its correct spot.
Trace example — array: [5, 2, 8, 1, 9]
A divide and conquer algorithm. Split the array in half, recursively sort each half, then merge the two sorted halves back together.
Mental model: Split until you have single elements (trivially sorted). Then merge pairs, then merge quadruples, etc.
Divide phase — visualize this for [5, 2, 8, 1]:
| Factor | Selection Sort | Insertion Sort | Merge Sort |
|---|---|---|---|
| Simplicity | Simple | Simple | Complex |
| Best for | Small arrays | Nearly sorted | Large arrays |
| Extra memory | O(1) | O(1) | O(n) |
| Swaps | At most n−1 | Many shifts | No in-place |
| Uses recursion? | No | No | Yes |
Given an intermediate array state, identify the algorithm:
The AP exam uses both. Searching and sorting work the same way conceptually, but syntax differs: