PrepGo

Informal Run-Time Analysis - AP Computer Science A Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 8 minutes to read.

Getting Started

Once a program correctly solves a problem, a new question arises: is it efficient? For the same problem, multiple correct algorithms can exist, but some may be significantly faster or use fewer resources than others. Informal run-time analysis provides a way to measure and compare the efficiency of algorithms by counting the number of operations they perform, allowing us to choose the best solution for a given task.

What You Should Be Able to Do

  • Count the number of times a key statement or operation is executed in a code segment.

  • Compare the efficiency of different algorithms that solve the same problem.

  • Determine if an algorithm's run time is constant, linear, or quadratic based on its statement execution count relative to the input size.

  • Analyze code that uses sequential statements, for loops, and nested for loops to determine its overall run time.

Key Concepts & Java Implementation

The Core Idea

In programming, run time doesn't refer to the time measured by a clock (in seconds or milliseconds), as that can change based on the computer's speed. Instead, we analyze run time by counting the number of basic steps or operations an algorithm performs. This is called the statement execution count. By understanding how this count changes as the amount of input data grows, we can classify an algorithm's efficiency.

For example, if we have an array of n elements, we want to know if the number of steps our algorithm takes stays the same, grows proportionally to n, or grows even faster (like n * n). This gives us a powerful, hardware-independent way to compare algorithms. We can group algorithms into three main categories of efficiency.

Analyzing Different Runtimes

The primary way to analyze an algorithm's efficiency is to examine its structure, particularly its loops, in relation to the size of its input.

1. Constant Run Time

An algorithm has a constant run time if the number of operations it performs does not change, regardless of the size of the input. These are the most efficient operations.

  • Identification: Look for code with no loops that depend on the input size. This includes simple arithmetic operations, assignments, and accessing a single element in an array or ArrayList by its index.

  • Annotated Java Example:

    
    // This method swaps the first two elements of an array.
    
    // The input is an integer array `data`.
    
    public void swapFirstTwo(int[] data) {
    
        // Check to ensure the array is large enough. This is one operation.
    
        if (data.length < 2) {
    
            return;
    
        }
    
    
        // The following three lines are the core of the swap.
    
        // They execute exactly once, no matter how large `data` is.
    
        int temp = data[0]; // 1. Assignment/read
    
        data[0] = data[1];  // 2. Assignment/read
    
        data[1] = temp;     // 3. Assignment/read
    
    }
    

    Analysis: The swapFirstTwo method performs a fixed number of operations (a check and three assignments). Whether the array has 2 elements or 2 million elements, the execution count remains the same. This is constant time.

2. Linear Run Time

An algorithm has a linear run time if the number of operations is directly proportional to the size of the input (n). If the input size doubles, the number of operations also roughly doubles.

  • Identification: Look for a single for or while loop that iterates through all n elements of the input.

  • Annotated Java Example:

    
    // This method finds the maximum value in an integer array.
    
    // The input size `n` is `data.length`.
    
    public int findMax(int[] data) {
    
        if (data.length == 0) {
    
            return -1; // Or throw an exception
    
        }
    
    
        int max = data[0]; // Executes once
    
    
        // This loop runs data.length - 1 times.
    
        // Let n = data.length. The loop runs n-1 times.
    
        for (int i = 1; i < data.length; i++) {
    
            // This comparison is the key operation inside the loop.
    
            if (data[i] > max) {
    
                max = data[i]; // This line only runs sometimes, but we analyze the worst case.
    
            }
    
        }
    
        return max; // Executes once
    
    }
    

    Analysis: The key factor is the for loop. The number of times the comparison data[i] > max executes is directly tied to data.length. If the array has 10 elements, the loop runs 9 times. If it has 1,000 elements, it runs 999 times. The execution count grows linearly with the input size n.

3. Quadratic Run Time

An algorithm has a quadratic run time if the number of operations is proportional to the square of the size of the input (n^2). If the input size doubles, the number of operations roughly quadruples.

  • Identification: Look for a nested loop structure, where both the outer and inner loops iterate a number of times dependent on the input size n.

  • Annotated Java Example:

    
    // This method checks if an array contains any duplicate values.
    
    // The input size `n` is `data.length`.
    
    public boolean containsDuplicate(int[] data) {
    
        // The outer loop runs n times (from i = 0 to n-1).
    
        for (int i = 0; i < data.length; i++) {
    
            // The inner loop runs approximately n times for EACH outer loop iteration.
    
            for (int j = i + 1; j < data.length; j++) {
    
                // This comparison is the key operation.
    
                // It will be executed roughly n*n / 2 times.
    
                if (data[i] == data[j]) {
    
                    return true; // Found a duplicate
    
                }
    
            }
    
        }
    
        return false; // No duplicates found
    
    }
    

    Analysis: For an array of size n, the outer loop runs n times. For each of those n iterations, the inner loop also runs a number of times proportional to n. The total number of comparisons is therefore proportional to n * n, or n^2. This is a quadratic run time and is significantly less efficient than linear time for large inputs.

Tracing & Analysis

Let's trace the statement execution count for finding a value in an array. The key operation is the comparison arr[i] == target.

Code to Trace:


public boolean findValue(int[] arr, int target) {

    for (int i = 0; i < arr.length; i++) {

        if (arr[i] == target) { // Key operation

            return true;

        }

    }

    return false;

}

Execution Trace Table:

Input: arr = {10, 50, 30, 70}, target = 30

iarr[i]arr[i] == target?Comparison Count
01010 == 30 (false)1
15050 == 30 (false)2
23030 == 30 (true)3
--Method returns trueTotal: 3

Analysis: In this case, the algorithm took 3 steps. However, to analyze efficiency, we consider the worst-case scenario. The worst case for this algorithm is when the target is not in the array, forcing the loop to run arr.length times. Therefore, the algorithm has a linear run time, as its worst-case performance is directly proportional to the array's size.

Java Syntax Quick-Reference

This topic analyzes existing Java syntax rather than introducing new keywords. The key structures to analyze are:

  • statement;: A single line of code (e.g., int x = 5;, y = y + 1;) is typically counted as one operation.

  • for (int i = 0; i < n; i++) { ... }: A loop that executes its body n times. The total cost is n times the cost of the loop's body.

  • for (...) { for (...) { ... } }: A nested loop. If both loops depend on an input size n, the body of the inner loop executes approximately n^2 times.

Core Code Examples & Terminology

  • Run Time: A measure of an algorithm's efficiency, determined by the number of operations it performs as a function of its input size.

  • Statement Execution Count: The total number of times fundamental operations or lines of code are executed while an algorithm is running.

  • Constant Time: An algorithm's run time is constant if its statement execution count does not depend on the size of the input.

  • Linear Time: An algorithm's run time is linear if its statement execution count is directly proportional to the input size, n.

  • Quadratic Time: An algorithm's run time is quadratic if its statement execution count is proportional to the square of the input size, n^2.

  • Core Snippet 1 (Linear Search):

    
    int count = 0;
    
    for (int i = 0; i < arr.length; i++) {
    
        if (arr[i] > 100) {
    
            count++;
    
        }
    
    }
    

    This code snippet has a linear run time because the single for loop iterates through the entire array once.

  • Core Snippet 2 (Quadratic Pair Check):

    
    for (int i = 0; i < arr.length; i++) {
    
        for (int j = 0; j < arr.length; j++) {
    
            System.out.println(arr[i] + ", " + arr[j]);
    
        }
    
    }
    

    This code snippet has a quadratic run time because the nested loops cause the println statement to execute arr.length * arr.length times.

Core Skill Check

  • Code Tracing: What is the final value of counter after this Java code runs: int counter = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { counter++; } }?

    • Answer: 15
  • Analysis: Identify the run time (constant, linear, or quadratic) of this method relative to n: public void doSomething(int n) { for (int i = 0; i < 100; i++) { System.out.println(i); } }.

    • Answer: Constant. The loop runs a fixed 100 times, regardless of the value of n.
  • Application: Write a single line of Java code that represents a constant-time operation on an ArrayList named list.

    • Answer: int x = list.get(0); (assuming the list is not empty).

Common Misconceptions & Errors

  • Confusing Run Time with Clock Time: Informal run time is about counting operations, not measuring seconds. A linear algorithm will always be considered more efficient than a quadratic one for large inputs, even if the quadratic one is faster on a specific computer for a very small input.

  • Assuming All Loops are Linear: A loop that runs a fixed number of times (e.g., for (int i = 0; i < 10; i++)) is a constant time operation. Its execution count does not depend on the input size.

  • Ignoring Method Call Costs: When a loop calls a method, the run time of that method must be included in the analysis. A linear loop that calls a linear method results in a quadratic overall run time.

  • Miscounting Sequential Loops: Two separate, non-nested loops that each run n times result in a total of n + n = 2n operations. This is still considered a linear run time, not quadratic.

Summary

Informal run-time analysis is a fundamental skill for evaluating the efficiency of code. By counting the number of key operations, we can classify algorithms based on how their performance scales with the size of the input. The three primary categories are constant, linear, and quadratic. Constant time algorithms are the fastest, as their execution time is independent of input size. Linear time algorithms, typically involving a single loop over the data, are very common and generally efficient. Quadratic time algorithms, often characterized by nested loops, can become very slow as the input size grows and should be used with caution.