AP CSP Algorithmic Efficiency
AP CSP Algorithmic Efficiency: Complete Guide (2025‑2026)
Algorithmic efficiency measures how a program’s resource usage (time, steps) grows as the input size increases. AP CSP focuses on reasonable vs. unreasonable time. Reasonable time (polynomial or better) means the algorithm is feasible for large inputs. Unreasonable time (exponential, factorial) means it becomes unusable quickly. The exam tests which algorithms are reasonable, how nested loops affect efficiency, and why some problems have no known efficient solution.
Contents
Growth Rates and Reasonable Time
The gap between O(N) and O(N²) grows dramatically. For N=1,000: linear needs 1,000 steps; quadratic needs 1,000,000 steps.
- O(1): constant — always same steps
- O(log N): logarithmic — binary search
- O(N): linear — linear search, FOR EACH
- O(N²): quadratic — nested loops
- All are considered ‘reasonable’ on the AP exam
- O(2^N): doubles with each added element
- O(N!): factorial — TSP brute force
- N=10: 2^10=1024, N=50: 2^50 > 1 quadrillion
- No hardware improvement makes this feasible
- Heuristics are used instead for these problems
Comparing Algorithms
Algorithm A takes 3N + 5 steps for a list of size N. Algorithm B takes N² steps. For N=10: A needs 35 steps, B needs 100. For N=100: A needs 305 steps, B needs 10,000.
Which algorithm is more efficient for large N? What growth rate does each have?
Algorithm A is O(N) — linear growth. Algorithm B is O(N²) — quadratic growth. For large N, Algorithm A is far more efficient even if B seems close for small inputs. The 3 and 5 constants in A’s formula are ignored in big-O notation: what matters is how the dominant term grows with N.
Code Trace: Counting Steps
How many times does DISPLAY execute? What is the growth rate?
5 times (once per element = N times) DISPLAY runs once per element. For N elements: N steps. Growth rate: O(N) — linear.
How many DISPLAY executions? What is the growth rate?
16 times (4 * 4 = N * N = N²) Outer loop: 4 iterations. Inner loop: 4 per outer. Total: 4×4=16. For general N: N×N=N² steps. Growth rate: O(N²) — quadratic.
For a sorted list of 128 elements, what is the maximum number of binary search comparisons?
7 log2(128) = 7. 128→64→32→16→8→4→2→1. 7 steps.
Is brute-force password cracking reasonable time?
Unreasonable Growth is exponential: 26^N where N is password length. This grows faster than any polynomial. Modern computers cannot try 141 trillion combinations in any useful time. This is why exponential time is classified as unreasonable.
Spot the Inefficiency
The inner loop is unnecessary. Checking if x=5 does not need to iterate over bigList again. This is O(N²) when O(N) is sufficient. Fix: remove the inner loop. A single FOR EACH over bigList is enough to find a value.
Common Exam Pitfalls
AP CSP defines reasonable time as polynomial or better. O(N²) is slow but reasonable. Only exponential (2^N) and factorial (N!) are unreasonable.
Each outer iteration runs the inner loop N times. Total = N × N = N². Not N + N = 2N.
If an algorithm needs 2^N steps, doubling computer speed only adds 1 to the maximum feasible N. Hardware improvements cannot overcome exponential growth.
Binary search halves the remaining elements each step. For 1,024 elements: 10 steps (log2), not 512 (N/2).
Check for Understanding
1. A single FOR EACH loop over a list of N elements executes the body how many times?
- 1
- N
- N/2
- N²
2. Two nested REPEAT N TIMES loops (one inside the other) execute the innermost body how many times?
- 2N
- N+N
- N²
- N*2
3. Which algorithm has unreasonable time complexity?
- Linear search on a list of N elements
- Binary search on a sorted list of N elements
- Checking every possible subset of N items (2^N subsets)
- A nested loop iterating N² times
4. Consider: I. An O(N) algorithm is always faster than an O(N²) algorithm. II. Increasing hardware speed can make exponential-time algorithms reasonable. III. Binary search runs in O(log N) time.
- I only
- III only
- I and III only
- I, II, and III
5. For N=1,000, which algorithm requires the MOST steps?
- O(N): linear search
- O(log N): binary search
- O(N²): nested loop sort
- O(1): accessing list[1]
6. A program doubles the number of steps required each time N increases by 1. This is:
- O(N) — linear
- O(N²) — quadratic
- O(2^N) — exponential and unreasonable
- O(log N) — logarithmic
7. Why is heuristic used for the Traveling Salesperson Problem instead of an exact algorithm?
- Heuristics always find the optimal solution faster.
- The exact solution requires checking N! routes, which is unreasonable time for large N.
- The problem has no solution for lists larger than 20 cities.
- Heuristics use less memory than exact algorithms.
8. A sorted list of 256 elements is searched with binary search. What is the maximum number of comparisons?
- 8
- 16
- 128
- 256
9. Two algorithms solve the same problem. Algorithm A takes 50N steps; Algorithm B takes N²/10 steps. For N=1,000, which is faster?
- Algorithm A: 50,000 steps vs B: 100,000 steps — A is faster
- Algorithm B because it divides by 10
- They are the same because both are polynomial
- Algorithm A only for small N
10. Which statement about algorithmic efficiency is most accurate?
- A program that runs in O(N²) time is considered unreasonable on the AP exam.
- Reasonable time includes any polynomial complexity (O(1), O(log N), O(N), O(N²), etc.)
- Only O(1) and O(N) are considered reasonable time.
- Doubling computer speed doubles the maximum feasible input size for any algorithm.
How the AP Exam Tests This
- Determine whether an algorithm runs in reasonable or unreasonable time
- Count steps in a nested loop structure (inner * outer = N²)
- Calculate log2(N) for binary search comparison count
- I/II/III: which statements about efficiency and growth rates are correct
- Identify which of two algorithms is more efficient for large N
FAQ
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]