Algorithmic
Efficiency
Learn how to measure, compare, and reason about the time and resources algorithms require as inputs grow.
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.
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.
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 \).
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.
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.
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.
FOR EACH item IN list
IF item = target
RETURN true
RETURN false
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.
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
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
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
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
Worked Example: Counting Steps
Consider this algorithm that finds the maximum value in a 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:
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):
// That's 2 choices per element → 2 × 2 × ... × 2
// Total subsets = \( 2^n \)
Common Mistakes
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?