Lesson 4.13: Searching and Sorting
Lesson 4.13: Searching and Sorting
What You'll Learn
- 4.13.A: Apply linear and binary search algorithms to find values in a list.
- 4.13.B: Apply selection sort and insertion sort to order a list.
- Explain why binary search requires a sorted array.
- Trace selection sort and insertion sort step by step.
- Compare the efficiency of linear vs. binary search.
Algorithm Summary
| Algorithm | Requires Sorted? | How It Works |
|---|---|---|
| Linear Search | No | Check each element one by one from left to right |
| Binary Search | Yes — ascending order | Check the middle; eliminate half the remaining elements each step |
| Selection Sort | No (produces sorted) | Find the minimum of the unsorted portion, swap it into place |
| Insertion Sort | No (produces sorted) | Take the next unsorted element and insert it into the correct position in the sorted portion |
Linear Search (4.13.A)
Linear search scans every element from left to right until the target is found or the list is exhausted. You already wrote this in Lesson 4.5 — same algorithm, same code.
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
Works on any array — sorted or unsorted. In the worst case, examines every element.
Binary Search (4.13.A)
Binary search only works on a sorted array. It checks the middle element, then eliminates half the remaining search range each step:
- Set
low = 0,high = arr.length - 1. - Compute
mid = (low + high) / 2. - If
arr[mid] == target, returnmid. - If
target < arr[mid], search left half:high = mid - 1. - If
target > arr[mid], search right half:low = mid + 1. - Repeat until found or
low > high(not found — return -1).
✅ Binary Search Trace: find 7 in {1, 3, 5, 7, 9, 11, 13}
low=0, high=6. mid=3. arr[3]=7. Found at index 3. Done in 1 step.
Now find 5: low=0, high=6. mid=3. arr[3]=7 > 5 → high=2. low=0, high=2. mid=1. arr[1]=3 < 5 → low=2. low=2, high=2. mid=2. arr[2]=5. Found at index 2.
⚠️ AP Exam Trap: Binary Search Requires Sorted Input
Running binary search on an unsorted array produces unpredictable results — it may return a wrong index or -1 even if the target exists. The AP exam tests this directly: if asked which search can be used on an unsorted array, the answer is always linear search, never binary search.
Selection Sort (4.13.B)
Selection sort builds the sorted portion from left to right. On each pass, it finds the minimum of the remaining unsorted portion and swaps it into the next position.
// Starting array: {5, 3, 8, 1, 4}
// Pass 1: min=1 (index 3), swap with index 0 → {1, 3, 8, 5, 4}
// Pass 2: min=3 (index 1), already in place → {1, 3, 8, 5, 4}
// Pass 3: min=4 (index 4), swap with index 2 → {1, 3, 4, 5, 8}
// Pass 4: min=5 (index 3), swap with index 3 → {1, 3, 4, 5, 8}
// Done
After each pass, one more element is in its final sorted position at the left end.
Insertion Sort (4.13.B)
Insertion sort grows a sorted region from left to right. On each step, it takes the next unsorted element and inserts it into the correct position within the already-sorted left portion — shifting elements right to make room.
// Starting array: {5, 3, 8, 1, 4}
// Step 1: take 3, insert before 5 → {3, 5, 8, 1, 4}
// Step 2: take 8, already in place → {3, 5, 8, 1, 4}
// Step 3: take 1, insert at front → {1, 3, 5, 8, 4}
// Step 4: take 4, insert between 3 and 5 → {1, 3, 4, 5, 8}
// Done
📌 Selection vs. Insertion Sort
Selection sort always makes exactly one swap per pass, finding the minimum first. Insertion sort shifts elements rather than swapping, and inserts from the right side of the sorted region. Both produce a correctly sorted array. The AP exam may ask you to identify which algorithm produced a given intermediate state — trace the key difference: selection grows from the left by swapping minimums; insertion grows from the left by shifting and inserting.
Summary
- Linear search: works on any array, checks elements one by one, returns index or -1.
- Binary search: requires sorted array, eliminates half the search range each step, much faster than linear for large arrays.
- Selection sort: finds the minimum of the unsorted portion and swaps it into place each pass.
- Insertion sort: takes the next element and inserts it into the correct position in the sorted portion.
Practice Questions
mid index examined?Mastery: Searching and Sorting
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]