AP Computer Science Principles · Study guide

Big Idea 3: Algorithms, Efficiency & Limits

Big Idea 3 · Algorithms & Programming · Part 4 of 4

Algorithms,
Efficiency & Limits

Linear and binary search, what makes an algorithm take reasonable time, heuristics for when exact solutions are too slow, and problems no algorithm can solve.

§1 What Is an Algorithm?

An algorithm is a finite sequence of steps that solves a problem. Every algorithm can be expressed using just three building blocks:

  • Sequencing — steps run in order.
  • Selection — steps run based on conditions (IF).
  • Iteration — steps repeat (loops).

These three constructs are enough to express any computable algorithm.

§2 Linear Search

Linear search checks each element one at a time from the start. Works on any list — sorted or not.

FOR EACH item IN list
{
  IF(item = target)
  {
    RETURN(true)
  }
}
RETURN(false)

Worst case: check every element. For n elements, up to n steps.

§3 Binary Search

Binary search is much faster but has a strict prerequisite: the list must be sorted. It works by repeatedly cutting the search range in half.

Critical prerequisite

Binary search requires the list to be sorted. On an unsorted list, it gives wrong answers. "Which list would binary search be LEAST likely to work on?" → the unsorted one.

3.1 Linear vs. Binary Comparison

Linear searchBinary search
Requires sorted?NoYes
Worst-case stepsnlog₂ n
n = 1,000up to 1,000about 10
When to useSmall or unsorted listsLarge sorted lists

§4 Algorithm Efficiency

4.1 Reasonable vs. Unreasonable Time

  • Reasonable time — steps grow slowly (n, n², n log n). Practical for large inputs.
  • Unreasonable time — steps grow as 2ⁿ or n!. Even small inputs take longer than a lifetime.

An algorithm is "reasonable" if its step count grows polynomially (n, n², n³). Exponential (2ⁿ) or factorial (n!) is unreasonable.

4.2 Heuristics

When an exact solution takes unreasonable time, a heuristic finds a "good enough" answer quickly. Heuristics don't guarantee the best answer, but they get a reasonable one without taking forever.

Example

Finding the shortest route visiting 50 cities exactly once has 50! ≈ 3 × 10⁶⁴ possible orderings. No computer can check them all. A heuristic like "always visit the nearest unvisited city" finds a route that's usually close to optimal, in seconds.

§5 Undecidable Problems

Some problems cannot be solved by any algorithm — not slowly, not approximately, not at all. These are undecidable problems.

A famous example: the halting problem. There is no algorithm that can always correctly determine whether any given program will eventually stop or run forever.

Definition · Unreasonable vs. Undecidable

Unreasonable time: an algorithm exists but is too slow to finish for large inputs.

Undecidable: no algorithm exists at all, no matter how much time is allowed.

These are fundamentally different. An unreasonable problem is solvable in theory. An undecidable problem is not.

§6 Exam Patterns

6.1 "For which list is binary search LEAST appropriate?"

The unsorted list. Binary search gives wrong answers on unsorted data.

6.2 "Which task benefits from a heuristic?"

The one with an exponential number of possibilities (2ⁿ, n!). If the exact solution requires checking every combination, a heuristic is appropriate.

6.3 "Which robot commands reach the goal?"

Draw the robot's path on the grid. Track position and facing direction after every step. Rule out choices that hit walls or go off the grid.

§7 Key Terms

TermDefinition
AlgorithmA finite sequence of steps that solves a problem
Binary searchSearch that halves a sorted range; log₂ n steps
HeuristicAn approximate approach that finds a good-enough answer quickly
Linear searchSearch that checks elements one at a time; n steps
Reasonable timeStep count grows polynomially with input size
Undecidable problemA problem no algorithm can solve for all inputs

§8 Self-Check

  1. A sorted list has 1024 elements. How many comparisons does binary search need in the worst case? Linear search?
  2. What is the difference between an algorithm that runs in "unreasonable time" and one that solves an "undecidable problem"?
  3. A procedure takes a list and must find the largest value. Should you use linear search or binary search? Why?
  4. When is a heuristic an appropriate solution to a problem?
Show Answers
  1. Binary: log₂ 1024 = 10 comparisons. Linear: up to 1024.
  2. Unreasonable time: an algorithm exists but takes impractically long for large inputs. Undecidable: no algorithm exists at all for all inputs (e.g., the halting problem).
  3. Linear search. Finding the largest requires looking at every element — there's no "halving" strategy. Also, binary search requires the list to be sorted, which is a stronger prerequisite than this problem needs.
  4. When an exact algorithm exists but takes unreasonable time (exponential or factorial), and a good-enough answer is acceptable. Examples: GPS routing, game AI, recommendation systems.