AP CSP Topic 3.11: Binary Search | Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 Topic 3.11: Binary Search
🎓54.5% score 5s vs 9.6% nationally
5.0 Wyzant — 451+ reviews
📍Blue Valley North, Overland Park KS
3.11
AP CSP — Big Idea 3: Algorithms & Programming
CED Aligned • Exam Ready

Topic 3.11: Binary Search

🎓 High School AP
💻 Python + AP Pseudocode
🎯 30-35% Exam Weight
📚 Interactive Lesson

🎯 Learning Objectives

  • Describe how binary search halves the search space at each step
  • State why binary search requires sorted input
  • Compare binary and linear search efficiency
  • Trace binary search steps to find a target value
📈
Exam Impact: Binary search is a foundational algorithm. The AP exam tests conceptual understanding and comparison with linear search.
💡 Why This Matters

To find a word in a dictionary you don't start at page 1. You open to the middle, decide left or right, repeat. Binary search finds any word in a 1,000-page dictionary in at most 10 checks.

How Binary Search Works

Binary search requires a sorted list. Each step cuts the remaining search space in half by checking the middle element.

  1. Find the middle element
  2. If it equals the target, done
  3. If target is less, search the left half
  4. If target is greater, search the right half
  5. Repeat until found or space is empty
data = [2,5,8,12,16,23,38,56]
target = 23
low, high = 0, len(data)-1
while low <= high:
    mid = (low+high) // 2
    if data[mid] == target:
        print(mid); break
    elif data[mid] < target:
        low = mid + 1
    else:
        high = mid - 1
data <- [2,5,8,12,16,23,38,56]
target <- 23
low <- 1
high <- LENGTH(data)
REPEAT UNTIL low > high
{
    mid <- (low+high) DIV 2
    IF data[mid] = target
    { DISPLAY(mid) }
    ELSE IF data[mid] < target
    { low <- mid+1 }
    ELSE { high <- mid-1 }
}

Binary vs Linear Efficiency

Linear search worst case: n checks. Binary search worst case: log₂n checks.

  • 8 elements: at most 3 checks
  • 1,000 elements: at most 10 checks
  • 1,000,000 elements: at most 20 checks

Tradeoff: binary search only works on sorted data.

Practice Problems

🔎 Practice MCQ
Binary search on a sorted list of 64 elements requires at most how many comparisons?
⚠️ Predict your answer BEFORE clicking.
🔎 Practice MCQ
Which is a requirement for binary search?
I. List must be sorted
II. List must contain only integers
III. List must have an odd number of elements
⚠️ Predict your answer BEFORE clicking.
🔎 Practice MCQ
Searching for 15 in [3,7,15,22,35] using binary search. First element checked?
⚠️ Predict your answer BEFORE clicking.
🎮 Game
Binary Search Step Tracer
Trace binary search steps. Pick the correct comparison. 8 questions.
0
Correct
1/8
Question
0
Streak 🔥
🤔 Evaluate:
0/8
correct
💻 Python Code Editor
Practice Problems
Write Python and check your answer. Paste into Replit for complex programs.
Problem 1 of 3
Print how many comparisons linear search needs to find 9 in [2,5,7,9,11]. Expected: 4
Hint: The loop checks 2,5,7,9 -- that's 4 comparisons before finding 9.
Problem 2 of 3
Print the index where 16 is found in [2,8,16,24,32]. Expected: 2
Hint: data[2]=16.
Problem 3 of 3
Spot the error: binary search misses target in the right half.
elif data[mid] < 23: high = mid - 1
Hint: When data[mid]

Frequently Asked Questions

Binary search assumes that if the target is less than the middle element, it must be in the left half. On an unsorted list this assumption fails -- the target could be anywhere.
low and high pointers eventually cross (low > high). Search space is empty. Algorithm returns -1 or a not-found indicator.
Yes, for unsorted data or a single search. Sorting takes time. If you search once, linear on unsorted may be faster than sort+binary. For repeated searches on the same data, sort once then use binary.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas — $249
Get the Superpack →

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]