Lesson 4.13: Searching and Sorting

Unit 4 · Lesson 4.13 · Algorithms

Lesson 4.13: Searching and Sorting

🕑 45–55 min · 10 Practice Questions · Linear Search · Binary Search · Selection Sort · Insertion Sort

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:

  1. Set low = 0, high = arr.length - 1.
  2. Compute mid = (low + high) / 2.
  3. If arr[mid] == target, return mid.
  4. If target < arr[mid], search left half: high = mid - 1.
  5. If target > arr[mid], search right half: low = mid + 1.
  6. 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

MCQ 1
An array contains {2, 5, 8, 11, 14, 17, 20}. Binary search is used to find the value 11. What is the first mid index examined?
Apply the binary search formula before reading the options.
A 2
B 3
C 4
D 0
B. low=0, high=6. mid = (0+6)/2 = 3 (integer division). arr[3] = 11 — found immediately at index 3 on the first step.
MCQ 2
Which statement about binary search is accurate?
A Binary search works on any array regardless of order.
B Binary search always examines every element.
C Binary search requires a sorted array and eliminates half the remaining elements each step.
D Binary search is always slower than linear search.
C. The two defining properties of binary search: requires sorted input, and halves the search space each step. A is wrong — unsorted input produces unreliable results. B describes linear search. D is the opposite — binary search is much faster for large sorted arrays.
MCQ 3
After one complete pass of selection sort on {9, 3, 7, 1, 5}, what does the array look like?
Find the minimum, then trace the swap.
A {3, 9, 7, 1, 5}
B {1, 3, 7, 9, 5}
C {3, 1, 7, 9, 5}
D {1, 3, 7, 9, 5}
D. Pass 1: find minimum of entire array (1 at index 3), swap with index 0 → {1, 3, 7, 9, 5}. Wait — after swapping 9 and 1: {1, 3, 7, 9, 5}. That matches D. Note: A shows swapping 9 and 3 (wrong — 1 is the minimum).
MCQ 4
After the first step of insertion sort on {6, 2, 8, 4, 1}, what does the array look like?
Take the second element and insert it correctly.
A {2, 6, 8, 4, 1}
B {1, 2, 8, 4, 6}
C {6, 2, 4, 8, 1}
D {2, 6, 4, 8, 1}
A. Insertion sort starts with index 1 (value 2). Sorted region is {6}. 2 < 6, so shift 6 right and insert 2 before it → {2, 6, 8, 4, 1}. Only the first two elements are affected in step 1.
MCQ 5
A student wants to search an unsorted array for a specific value as quickly as possible. Which algorithm should they use?
A Binary search, because it is always faster
B Selection sort, then binary search
C Linear search, because the array is unsorted
D Insertion sort, because it is faster than linear search
C. Binary search requires sorted input — it cannot be applied to an unsorted array. Linear search is the only correct option. B would work but is slower overall because sorting first takes additional time. D describes a sorting algorithm, not a search.
Tier 3 · AP Mastery

Mastery: Searching and Sorting

MCQ 6
Binary search is used to find 10 in {1, 4, 7, 10, 13, 16, 19, 22}. How many elements are examined before 10 is found?
Trace binary search step by step.
A 1
B 2
C 4
D 8
A. low=0, high=7. mid=(0+7)/2=3. arr[3]=10 — found immediately on the first step. Exactly 1 element examined.
MCQ 7
After two complete passes of selection sort on {4, 2, 9, 1, 6}, what does the array look like?
Trace both passes carefully.
A {1, 2, 3, 4, 6}
B {1, 2, 9, 4, 6}
C {1, 2, 9, 4, 6}
D {1, 4, 9, 2, 6}
C. Pass 1: min of {4,2,9,1,6}=1 at index 3. Swap with index 0 → {1,2,9,4,6}. Pass 2: min of {2,9,4,6}=2 at index 1. Already at index 1 (no swap needed) → {1,2,9,4,6}. After two passes the first two positions are sorted.
MCQ 8
Which intermediate state could result from insertion sort but NOT selection sort after two steps on {7, 3, 5, 1, 9}?
Think about the key difference between the two algorithms.
A {1, 3, 5, 7, 9}
B {1, 3, 5, 7, 9}
C {1, 7, 5, 3, 9}
D {3, 5, 7, 1, 9}
D. Insertion sort after 2 steps on {7,3,5,1,9}: step 1 takes 3, inserts before 7 → {3,7,5,1,9}. Step 2 takes 5, inserts between 3 and 7 → {3,5,7,1,9}. Selection sort after 2 steps: pass 1 moves 1 to front, pass 2 moves 3 to second — elements at the front are globally smallest. D ({3,5,7,1,9}) has the first three elements sorted but 1 still in the unsorted region — this is the insertion sort pattern, not selection sort.
MCQ 9
Binary search is applied to {3, 6, 9, 12, 15, 18} to find the value 18. What sequence of mid indices is examined?
Trace every step of the search.
A 2, 4, 5
B 3, 5
C 0, 3, 5
D 2, 3, 5
A. low=0, high=5. mid=(0+5)/2=2. arr[2]=9 < 18 → low=3. low=3, high=5. mid=(3+5)/2=4. arr[4]=15 < 18 → low=5. low=5, high=5. mid=5. arr[5]=18. Found. Indices examined: 2, 4, 5.
MCQ 10
What is the key difference between selection sort and insertion sort?
A Selection sort works on unsorted arrays; insertion sort requires sorted input.
B Insertion sort always makes exactly one swap per pass.
C Selection sort finds the minimum and swaps it into place; insertion sort takes the next element and inserts it into the correct position in the sorted region.
D They are functionally identical — both produce the same intermediate states.
C. This is the defining distinction the AP exam tests. Selection sort: scan unsorted portion for minimum, swap into position. Insertion sort: take next unsorted element, shift sorted elements right until correct insertion point is found. A is wrong — both work on unsorted input. B describes selection sort (one swap per pass), not insertion sort. D is wrong — they produce different intermediate states.

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]