Linear Search, Binary Search

Computer Science A tests two searching algorithms.

What to practice

  • Trace exact values of low, high, and mid and count comparisons in Binary Search.
  • Identify intermediate array states and count comparisons/swaps/shifts in Selection Sort and Insertion Sort.
  • Compare best-case and worst-case behaviors of Insertion Sort (O(n) shifts when already sorted) vs. Selection Sort (always n(n-1)/2 comparisons).

Current CED topics: 4.14, 4.15, 4.17

Current learning objectives: 4.14.A, 4.15.A, 4.17.B

1. Core Concept Instruction

AP Computer Science A tests two searching algorithms and two quadratic O(n^2) sorting algorithms: (1) Linear Search inspects elements sequentially from index 0 and works on any array (sorted or unsorted) in at most N comparisons. (2) Binary Search requires the array to be SORTED first; each iteration compares target to arr[mid] where mid = (low + high) / 2, cutting the search interval in half (if target < arr[mid], high = mid - 1; if target > arr[mid], low = mid + 1), taking at most floor(log_2 N) + 1 comparisons. (3) Selection Sort makes N - 1 passes: on pass i (from 0 to N - 2), it scans [i..N-1] for the minimum element and performs ONE swap to place it at index i. (4) Insertion Sort starts at i = 1 and inserts arr[i] into its proper sorted position among the already-sorted prefix [0..i-1] by shifting larger elements one index right.

2. Worked Examples

Worked Binary Search Midpoint Trace & Selection vs. Insertion Sort Pass Comparison

Part 1: Trace low, high, and mid when Binary Search looks for target = 42 in sorted array arr = {10, 18, 25, 31, 37, 42, 55, 68} (indices 0..7). Part 2: Given {40, 15, 30, 10, 25}, compare the array state after 2 passes of Selection Sort vs. 2 passes of Insertion Sort.
  1. Step 1 (Binary Search Iteration 1): Initially low = 0, high = 7 -> mid = (0 + 7)/2 = 3. Since arr[3] == 31 < 42, update low = mid + 1 = 4.
  2. Step 2 (Binary Search Iteration 2): Now low = 4, high = 7 -> mid = (4 + 7)/2 = 5. Since arr[5] == 42 == target, Binary Search returns index 5 after exactly 2 comparisons.
  3. Step 3 (Selection Sort on {40, 15, 30, 10, 25}): Pass 1 swaps min (10 at index 3) with index 0 -> {10, 15, 30, 40, 25}. Pass 2 finds min of [1..4] is 15 at index 1 (swaps with itself) -> {10, 15, 30, 40, 25}.
  4. Step 4 (Insertion Sort on {40, 15, 30, 10, 25}): Pass 1 (i = 1) inserts 15 before 40 -> {15, 40, 30, 10, 25}. Pass 2 (i = 2) inserts 30 between 15 and 40 -> {15, 30, 40, 10, 25}.

Result:

Insertion Sort vs. Selection Sort Pass-by-Pass Array State Trace

Trace the array int[] nums = {9, 4, 7, 2} after completing the first two outer-loop passes of Insertion Sort.
  1. Step 1 (Pass 1, insert index 1 value 4): 4 < 9, so 9 shifts right and 4 is inserted at index 0 -> {4, 9, 7, 2}.
  2. Step 2 (Pass 2, insert index 2 value 7): 7 < 9, so 9 shifts right and 7 is inserted at index 1 -> {4, 7, 9, 2}.

Result: After 2 passes of Insertion Sort: {4, 7, 9, 2}

Use this chapter companion

Review the lesson, attempt the original practice, then check the explanation. Your printed study guide remains complete without the website.

Independence

Skill by Skill Prep is independent. SAT® and AP® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website or book. PSAT/NMSQT® is a registered trademark of the College Board and the National Merit Scholarship Corporation, which are not affiliated with, and do not endorse, this site. Bluebook™ is a trademark of the College Board. ACT® is a trademark of ACT Education Corp., which is not affiliated with and does not endorse this website or book. Desmos is a trademark of Desmos Studio PBC. Practice results describe these questions, not official scores or admission predictions.

Last updated .