01 / 21

Algorithmic
Efficiency

Learn how to measure, compare, and reason about the time and resources algorithms require as inputs grow.

AP Computer Science Principles

What Is an Algorithm?

An algorithm is a finite sequence of well-defined instructions designed to solve a specific problem or perform a computation. Every app, search engine, and social media feed runs on algorithms.

Input
Data the algorithm receives: a list, a number, a graph, etc.
Process
Steps it follows: compare, swap, recurse, iterate.
Output
The result: a sorted list, a shortest path, a yes/no answer.

Why Efficiency Matters

Algorithmic efficiency describes how the number of steps an algorithm takes grows relative to the size of its input. A small difference in efficiency can mean the difference between a program finishing in 1 second or 1 billion years.

Input Size (n) Steps \( O(n) \) \( O(n^2) \) \( O(2^n) \) 0 n

Measuring Efficiency

We don't measure efficiency by wall-clock time (which varies by hardware). Instead, we count the number of fundamental operations as a function of input size \( n \).

1
Identify the input size \( n \): the length of a list, number of nodes, etc.
2
Count the dominant operations: comparisons, swaps, recursive calls.
3
Express the count as a function of \( n \), then simplify to its growth rate.

Big-O Notation

Big-O notation describes the upper bound on the growth rate of an algorithm. It tells us how the number of steps scales as \( n \to \infty \), ignoring constants and lower-order terms.

Drop Constants
\( O(5n) = O(n) \). The coefficient doesn't matter at scale.
Drop Lower Terms
\( O(n^2 + n) = O(n^2) \). The largest term dominates.

Common Growth Rates

Big-O Name Example Speed
\( O(1) \) Constant Array index lookup Fastest
\( O(\log n) \) Logarithmic Binary search Very fast
\( O(n) \) Linear Linear search Moderate
\( O(n \log n) \) Linearithmic Merge sort Moderate
\( O(n^2) \) Quadratic Bubble sort Slow
\( O(2^n) \) Exponential Brute-force subsets Impractical

Reasonable vs Unreasonable

The AP CSP framework divides algorithms into two categories based on whether they can finish in a practical amount of time.

Reasonable (Polynomial)
Runs in \( O(n^k) \) for some constant \( k \). Examples: \( O(1) \), \( O(n) \), \( O(n^2) \), \( O(n^3) \). These are feasible for large inputs.
Unreasonable (Super-Polynomial)
Runs in \( O(2^n) \), \( O(n!) \), etc. The number of steps explodes so fast that even \( n = 100 \) is beyond any computer.

Example: Linear Search

Given a list of \( n \) elements, linear search checks each element one by one until it finds the target or reaches the end.

PROCEDURE linearSearch(list, target)
  FOR EACH item IN list
    IF item = target
      RETURN true
  RETURN false
3 7 1 9 5 2 8 1st 2nd 3rd Found!

Example: Binary Search

Binary search works on a sorted list. It repeatedly divides the search space in half, eliminating half the remaining elements each step.

PROCEDURE binarySearch(sortedList, target)
  low ← 0, high ← LENGTH(sortedList) - 1
  REPEAT UNTIL \( \text{low} > \text{high} \)
    mid ← (low + high) / 2
    IF sortedList[mid] = target
      RETURN mid
    ELSE IF \( \text{sortedList}[\text{mid}] < \text{target} \)
      low ← mid + 1
    ELSE
      high ← mid - 1
  RETURN -1

A sorted collection makes discarded search ranges meaningful

An archivist opens a specimen drawer beside unordered return trays.Sorted order makes it possible to discard an entire search range after one comparison. An unsorted collection does not support the same elimination.

Binary search with AP-style list indices

Three search ranges use positions 1 through 9 and narrow to target 23 at position 6.This example uses AP-style list positions 1 through 9. Searching for 23 tests the midpoint values 17, 31 and 23 and returns position 6. The list is sorted before searching.

Count comparisons as the list grows

A step graph plots the exact worst-case binary-search probe count against list size on a log₂ horizontal axis.For inclusive midpoint binary search on n ≥ 1 items, the maximum number of probes is floor(log₂ n) + 1. The horizontal axis uses a log₂ scale; the count measures search probes after sorting.

Linear vs Binary Search

Property Linear Search Binary Search
Requires sorted data? No Yes
Worst-case steps \( n \) \( \log_2 n \)
Big-O \( O(n) \) \( O(\log n) \)
\( n = 1{,}000{,}000 \) 1,000,000 steps ~20 steps
Category Reasonable Reasonable

How to Determine Efficiency

1
Identify the input size \( n \): What grows? List length, number of cities, etc.
2
Find the loops: A single loop over \( n \) items → \( O(n) \). A loop inside a loop → \( O(n^2) \). Halving each step → \( O(\log n) \).
3
Combine nested structures: Nested loops multiply. Sequential sections add (keep the largest).
4
Simplify: Drop constants and lower-order terms. \( 4n^2 + 10n + 7 \) becomes \( O(n^2) \).
5
Classify: Is the result polynomial (\( n^k \))? Then reasonable. Exponential (\( 2^n \), \( n! \))? Then unreasonable.

Worked Example: Counting Steps

Consider this algorithm that finds the maximum value in a list:

PROCEDURE findMax(list)
  max ← list[0] ← 1 operation
  FOR EACH item IN list ← repeats n times
    IF \( \text{item} > \text{max} \) ← 1 comparison each
      max ← item ← at most 1 assignment
  RETURN max

Worked Example: Nested Loops

Consider an algorithm that checks all pairs in a list for duplicates:

PROCEDURE hasDuplicates(list)
  FOR i ← 0 TO LENGTH(list) - 1 ← n iterations
    FOR j ← i + 1 TO LENGTH(list) - 1 ← up to n iterations
      IF list[i] = list[j]
        RETURN true
  RETURN false

Worked Example: Exponential Growth

Imagine generating all possible subsets of a set with \( n \) elements (the power set):

// For each element, decide: include or exclude
// That's 2 choices per element → 2 × 2 × ... × 2
// Total subsets = \( 2^n \)
n \( 2^n \) subsets 5 32 10 1,024 20 1,048,576 50 \( \approx 10^{15} \)

Common Mistakes

✕ Confusing Speed with Efficiency
A faster computer doesn't change an algorithm's Big-O. \( O(n^2) \) on a supercomputer is still \( O(n^2) \).
✕ Forgetting to Drop Constants
\( O(3n) \) is just \( O(n) \). Constants don't affect the growth category.
✕ Calling \( O(n^2) \) Unreasonable
Polynomial algorithms (\( O(n^k) \)) are always reasonable, even if slow. Only exponential/factorial are unreasonable.

Summary of Key Concepts

Concept Key Idea
Algorithmic Efficiency How steps grow relative to input size \( n \)
Big-O Notation Upper bound on growth rate, ignoring constants
Reasonable Polynomial: \( O(1), O(n), O(n^2), O(n^3), O(n \log n) \)
Unreasonable Super-polynomial: \( O(2^n), O(n!) \)
Linear Search \( O(n) \): checks every element
Binary Search \( O(\log n) \): halves the search space each step

Question 1 of 5

An algorithm has a running time of \( 5n^3 + 2n + 100 \). What is its Big-O efficiency?

Question 2 of 5

Binary search requires that the data is sorted. If a list has 1,024 elements, what is the maximum number of comparisons binary search will make?

Question 3 of 5

Which of the following algorithms would be classified as having an unreasonable running time?

Question 4 of 5

An algorithm contains two nested loops, each iterating \( n \) times, followed by a single loop that iterates \( n \) times. What is the overall Big-O?

Question 5 of 5

A programmer doubles the speed of their computer. An algorithm with \( O(2^n) \) efficiency was previously able to handle \( n = 40 \) in one hour. Approximately what input size can it now handle in one hour?