AP COMPUTER SCIENCE A • SELECTION AND ITERATION

Informal Run-Time Analysis

Learn to estimate how algorithm execution time grows as input size increases, without formal proofs.

Historical Context & Motivation

Before modern computers existed, mathematicians and logicians were already grappling with a fundamental question: given two procedures that solve the same problem, how do we determine which one is more efficient? As early as the 1930s, researchers recognized that the sheer number of elementary steps an algorithm requires—and how that count scales with the size of the input—is a far more revealing metric than simply timing a program on a particular machine. This insight gave rise to the field of algorithm analysis, which provides a hardware-independent way to compare the efficiency of competing solutions.

In the AP Computer Science A course, you are expected to perform informal run-time analysis—that is, you reason about execution counts by inspecting loops and conditional structures rather than writing formal mathematical proofs. The goal is to classify code fragments into broad growth categories such as constant, linear, quadratic, or cubic time, enabling you to predict performance bottlenecks before you ever press "Run."

1936
Turing Machines & Computability
Alan Turing formalizes the notion of an algorithm with his abstract machine model, laying the groundwork for measuring computational steps.
1965
Big-O Notation Enters CS
Juris Hartmanis and Richard Stearns publish foundational work on computational complexity, popularizing asymptotic notation (Big-O) in computer science.
1973
Knuth's Art of Computer Programming
Donald Knuth's seminal series systematizes algorithm analysis, making step-counting and growth-rate reasoning standard practice in CS education.
2003
AP Computer Science A Adopts Java
The College Board transitions the AP CS A exam to Java, embedding informal run-time analysis of loops and nested iterations as a core testable skill.

The central question that informal run-time analysis addresses is deceptively simple: if I double the size of my input, how many more operations will my code perform? Answering that question accurately allows you to choose between a nested-loop approach that might bring a server to its knees and a single-pass solution that finishes in milliseconds.

Core Principles & Definitions

Informal run-time analysis centers on counting the number of times key operations execute as a function of the input size, which we denote n. Rather than tracking every machine instruction, we focus on the dominant term—the part of the expression that grows fastest as n increases—and discard constant factors and lower-order terms. This approach yields an intuitive but powerful way to classify algorithms.

1

Statement Execution Count

Every statement inside a loop body executes once per iteration. Count how many times the loop runs as a function of n to find the total operations.
2

Nesting Multiplies

When one loop is nested inside another, the inner loop's iterations are multiplied by the outer loop's iterations. Two nested loops over n yield roughly n × n = n² operations.
3

Sequential Adds

Consecutive (non-nested) loops contribute their counts additively. An O(n) loop followed by another O(n) loop is still O(n), because we keep only the dominant term.
4

Drop Constants & Lower-Order Terms

An expression like 3n² + 7n + 12 simplifies to O(n²). Constant multipliers and smaller terms become negligible for large n.
5

Worst-Case Focus

On the AP exam, you typically analyze the worst-case scenario—the input arrangement that forces the algorithm to do the most work—unless stated otherwise.
KEY TAKEAWAY
Think of run-time analysis like estimating how long a road trip will take. You focus on the major highway segments (dominant term) and ignore the time spent backing out of your driveway (constant overhead). If the highway distance doubles, your travel time roughly doubles—that proportionality is what Big-O captures.

Visualizing Growth Rates

The diagram below plots operation count versus input size for the most common growth categories encountered on the AP CS A exam. Notice how O(1) remains flat regardless of n, while O(n²) curves upward steeply as n grows. This visual intuition is essential: even modest increases in n can cause quadratic algorithms to perform orders of magnitude more work than linear ones.

Growth rate curves for common algorithm complexities. The green O(1) line stays constant, the blue O(n) line grows linearly, and the pink O(n²) curve climbs dramatically as input size increases.

This chart reveals why algorithm selection matters enormously. At n = 10, the difference between O(n) and O(n²) is only a factor of 10—manageable. But at n = 10,000, O(n) performs 10,000 operations while O(n²) performs 100,000,000. In practical terms, a linear algorithm that finishes in one second would see its quadratic counterpart take nearly three hours on the same machine. Recognizing which growth category a code fragment belongs to is one of the most practically valuable skills in computer science.

Mathematical Framework

Although the AP CS A exam requires only informal analysis, it helps to understand the underlying counting formulas that justify our intuitive classifications. When you count loop iterations systematically, you produce closed-form expressions that you then simplify using asymptotic reasoning.

SINGLE LOOP
for (int i = 0; i < n; i++) → n iterations → O(n)
A single for loop that runs from 0 to n − 1 executes its body exactly n times. The run-time is linear in n.
NESTED LOOPS (INDEPENDENT)
Outer: n iterations × Inner: n iterations = n × n = n² → O(n²)
When the inner loop's iteration count does not depend on the outer loop's variable, the total is simply the product. This is quadratic time.
NESTED LOOPS (DEPENDENT — TRIANGULAR SUM)
∑ i from 0 to n−1 of i = 0 + 1 + 2 + … + (n−1) = n(n−1)/2 → O(n²)
When the inner loop runs from 0 to i (where i is the outer loop variable), the total is the triangular number n(n − 1)/2. Expanding gives (n² − n)/2; dropping the constant factor ½ and the lower-order term −n, we get O(n²).
TRIPLE NESTING
n × n × n = n³ → O(n³)
Three independently bounded nested loops over n produce cubic time. This pattern appears in naive matrix multiplication and some brute-force 3-sum algorithms.
💡 AP Exam Tip
The AP CS A exam will never ask you to write a formal proof or use limit definitions. Instead, you should be able to look at a code fragment, count how the loops nest, identify the dominant term, and select the correct Big-O classification from among O(1), O(n), O(n²), and occasionally O(n³) or O(log n).

Common Loop Patterns & Their Complexities

The AP exam draws from a predictable set of loop structures. Being able to recognize these patterns by sight—without having to trace every iteration—is the key skill tested in informal run-time analysis questions. The diagram below maps each common Java loop pattern to its complexity class, and the table that follows provides concrete iteration counts.

A visual map of common Java loop patterns and their Big-O classifications. Notice that both the independent nested loop (left box, second row) and the dependent inner loop (right box, second row) are both O(n²), even though the latter performs roughly half the iterations.
Iteration counts for n = 100 across common loop patterns
PatternCode StructureIterations (n = 100)Big-O
Direct accessarr[index]1O(1)
Single loopfor(i=0;i<n;i++)100O(n)
Halving loopwhile(x<n) x*=2;≈ 7O(log n)
Two nested loopsfor(i) for(j)10,000O(n²)
Dependent inner loopfor(i) for(j<i)4,950O(n²)
Three nested loopsfor(i) for(j) for(k)1,000,000O(n³)

Worked Example: Analyzing Nested Loops

Consider the following Java method. Our task is to determine its Big-O run-time in terms of n, the length of the array.

📝 Code Fragment
public static int mystery(int[] arr) { int n = arr.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { count++; } } } return count; }
Step-by-Step Run-Time Analysis
1
Step 1 — Identify the LoopsThe outer loop runs with i from 0 to n − 1, giving n iterations. The inner loop runs with j from i + 1 to n − 1, so its iteration count depends on i.
2
Step 2 — Count Inner Loop IterationsWhen i = 0, the inner loop runs n − 1 times. When i = 1, it runs n − 2 times. In general, when i = k, the inner loop runs n − 1 − k times. We sum these from k = 0 to n − 1:
Total = (n − 1) + (n − 2) + … + 1 + 0 = n(n − 1)/2
3
Step 3 — Expand and SimplifyExpanding: n(n − 1)/2 = n²/2 − n/2. The dominant term is n²/2.
4
Step 4 — Drop Constants and Lower-Order TermsWe discard the constant factor 1/2 and the lower-order term −n/2.
The method runs in O(n²) time.
5
Step 5 — Verify with the if-StatementThe if condition inside the inner loop is a constant-time comparison—it does not introduce additional iterations. Whether the condition is true or false, the comparison itself occurs every iteration. The conditional does not change the O(n²) classification.
KEY INSIGHT
Selection statements (if/else) inside a loop do not increase the loop's Big-O category, because the branch decision is O(1). Only additional nested loops or recursive calls can escalate the complexity class.

Common Traps & Exam Pitfalls

Students frequently misclassify loop structures on the AP exam due to several recurring misconceptions. The table below contrasts correct reasoning with common mistakes, helping you build a reliable mental checklist to avoid losing points.

Common misconceptions in informal run-time analysis
Trap / MisconceptionWhy It's WrongCorrect Reasoning
"The dependent inner loop is O(n), so total is O(n)"The inner loop runs a variable number of times per outer iteration. You must sum all inner executions, not just consider one pass.Sum 0 + 1 + 2 + … + (n − 1) = n(n − 1)/2 → O(n²)
"Two sequential loops means O(n²)"Sequential (non-nested) loops add, not multiply. O(n) + O(n) = O(2n) = O(n).Only nesting multiplies. Sequential loops use the max of their individual complexities.
"An if-statement doubles the complexity"A branch decision is O(1); it selects a path but doesn't add iterations.Analyze each branch separately and take the worst case. If both branches are O(1), the if-statement is O(1).
"The constant 100 in the inner bound makes it O(100n) = O(n²)"A fixed constant bound (like 100) does not grow with n.for(j=0; j<100; j++) inside a loop over n → 100 × n = O(n), not O(n²).
🎯 EXAM STRATEGY
Before classifying a code fragment, ask three questions: (1) How many loops are there and are they nested or sequential? (2) What does each loop bound depend on—n, a constant, or another loop variable? (3) Does any inner structure (like an if-statement) add extra iterations, or is it just O(1) overhead? This checklist catches virtually every trap the AP exam sets.

Connecting to Formal Complexity Theory

The informal analysis you perform on the AP exam is an entry point into the rich field of computational complexity theory. In university courses like Data Structures and Algorithms, you will encounter formal Big-O definitions involving limits, as well as companion notations like Big-Ω (lower bound) and Big-Θ (tight bound). You will also move beyond polynomial-time analysis to study logarithmic, exponential, and even factorial growth in the context of problems like sorting, graph traversal, and NP-completeness.

Informal vs. Formal Algorithm Analysis
AspectAP CS A (Informal)College CS (Formal)
MethodCount iterations by inspection; simplify by dropping lower-order termsProve upper bounds using limit definitions or recurrence relations
NotationBig-O only (upper bound)Big-O, Big-Ω, Big-Θ, little-o, little-ω
ScopeIterative loops (for, while)Recursion (Master Theorem), amortized analysis, probabilistic analysis
Growth ClassesO(1), O(n), O(n²), occasionally O(n³) or O(log n)All polynomial classes, O(2ⁿ), O(n!), complexity classes P and NP

Understanding informal analysis now gives you a massive head start. The intuition you build by eyeballing loop structures and reasoning about growth translates directly into the formal proofs and recurrence-solving techniques you will encounter in a university algorithms course. Think of informal analysis as learning to estimate distances on a map before you take a full course in surveying—the intuitive skill makes the precise technique far easier to learn.

Practice Problems

1
Which of the following best describes why we drop constant factors when expressing Big-O notation?
2
Consider the following code fragment: for (int i = 0; i < n; i++) { for (int j = 0; j < 10; j++) { System.out.println(i + j); } } What is the run-time complexity of this fragment?
3
What is the Big-O run-time of the following code? for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { doSomething(); // O(1) } } for (int k = 0; k < n; k++) { doSomethingElse(); // O(1) }
PROBLEM 4APPLIED
A teacher writes a method to check whether a list of n student names contains any duplicates: public static boolean hasDuplicates(String[] names) { for (int i = 0; i < names.length; i++) { for (int j = i + 1; j < names.length; j++) { if (names[i].equals(names[j])) { return true; } } } return false; } (a) State the Big-O worst-case run-time of this method. (b) Explain what input produces the worst case. (c) If the list has 1,000 names and each comparison takes 1 microsecond, estimate the maximum total time for the comparisons.
PROBLEM 5CRITICAL THINKING
Consider the following method: public static void process(int n) { for (int i = 1; i < n; i++) { int j = 1; while (j < n) { // O(1) work here j = j * 2; } } } (a) How many times does the outer for-loop execute as a function of n? (b) For a single iteration of the outer loop, how many times does the inner while-loop execute? Justify your answer. (c) What is the overall Big-O run-time of the method? Show your reasoning. (d) If the inner while-loop were changed to j = j + 1, what would the new Big-O be? Explain the difference.

Lesson Summary

Informal run-time analysis is the practice of estimating an algorithm's efficiency by counting how many times key operations execute as a function of the input size n. The fundamental technique involves identifying loops, determining whether they are nested (multiply) or sequential (add), computing the total iteration count, and then simplifying by dropping constant factors and lower-order terms to arrive at a Big-O classification.

The key complexity classes for AP CS A are O(1) constant, O(n) linear, O(n²) quadratic, and O(n³) cubic. Remember that selection statements (if/else) are O(1) and do not increase a loop's complexity class. A dependent inner loop (j goes from 0 to i) produces a triangular sum n(n − 1)/2, which is still O(n²). Finally, when loops are sequential rather than nested, take the maximum of their individual complexities as the overall run-time.

Varsity Tutors • AP Computer Science A • Informal Run-Time Analysis