Skip to main content
United StatesComputer Science PrinciplesSyllabus dot point

How does binary search beat linear search, and how do we compare the efficiency of algorithms?

Topic 3.11/3.17/3.18 Binary Search and Efficiency: binary search finds a value in a sorted list far faster than linear search, and algorithms are compared by efficiency, with some problems being unsolvable or only approximable.

A focused answer to AP CSP Topics 3.11, 3.17 and 3.18, covering linear versus binary search, why binary search needs a sorted list, reasonable versus unreasonable running time, polynomial versus exponential growth, heuristics, and undecidable problems.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Linear search
  3. Binary search
  4. Comparing efficiency
  5. Heuristics and undecidable problems
  6. Try this

What this topic is asking

The College Board (Topics 3.11, 3.17 and 3.18) wants you to compare search algorithms and reason about efficiency. You must contrast linear search (check each element in turn) with binary search (repeatedly halve a sorted list), and know that binary search is far faster but requires a sorted list. You also need the idea of reasonable versus unreasonable running time (polynomial versus exponential growth), heuristics (approximate solutions), and that some problems are undecidable (no algorithm can solve them).

For a sorted list of 1000, binary search needs about 10 comparisons because the list halves: 1000, 500, 250, 125, ... down to 1 in roughly 10 steps. Linear search could need 1000.

Comparing efficiency

Binary search (log) is more efficient than linear search (proportional to n), which is itself far better than exponential algorithms.

Heuristics and undecidable problems

When an exact solution would take unreasonable time, a heuristic provides an approximate, good-enough answer quickly (for example a delivery-route shortcut that is close to optimal). Some problems are undecidable: no algorithm can produce a correct yes/no answer for every possible input. These are limits on what computing can do.

Try this

Q1. Why is binary search faster than linear search on a large sorted list? [2 points]

  • Cue. Binary search halves the remaining list each step (about log-base-2 comparisons), while linear search may check every element (up to n), so binary search examines far fewer elements as the list grows.

Q2. What is a heuristic, and when is one useful? [2 points]

  • Cue. A heuristic is an approach that gives a good-enough approximate solution quickly; it is useful when finding the exact best answer would take unreasonable (for example exponential) time.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. A binary search is used to find a value in a sorted list of 1000 elements. Approximately how many elements must it examine in the worst case? (A) 1000 (B) 500 (C) About 10 (D) 1
Show worked answer →

The answer is (C).

Binary search halves the remaining list each step, so the worst case is about log base 2 of the list size. Log base 2 of 1000 is about 10, so binary search examines roughly 10 elements. (A) 1000 is the worst case for linear search. (B) 500 is the average for linear search. (D) 1 would only happen if the value were found immediately.

Markers reward knowing binary search halves the list each step, giving roughly log-base-2 comparisons, far fewer than linear search.

AP 2021 (style)2 marksFree response (short). Explain why a binary search cannot be used on a list that is not sorted, and what must be true of the list first.
Show worked answer →

A 2-point question on the precondition for binary search.

Point 1: Binary search works by comparing the target to the middle element and discarding half the list based on whether the target is larger or smaller. This only works if the list is sorted (ordered), so that "larger" reliably means "in the right half".

Point 2: If the list is unsorted, the half it discards might contain the target, so the result would be wrong. The list must be sorted first (which itself takes time). A common error is to claim binary search works on any list; it requires order.

Related dot points

Sources & how we know this