How to find the Big O of a nested loop

Determine the time complexity of a nested loop by evaluating the number of iterations for the inner loop and multiplying it by the number of iterations of the outer loop. This applies when evaluating standard iterative algorithms where loop bounds are defined by the input size nn.

The setup

Identify the outer loop, the inner loop, and the operations performed inside the deepest nesting level. Define the loop variable bounds and step sizes in terms of the input size nn.

The steps

  1. Determine the iteration count of the outer loop as a function of nn. 2. Determine the iteration count of the inner loop. 3. If the inner loop bounds are independent of the outer loop, multiply the two counts. If the inner loop depends on the outer loop variable, express the total iterations as a summation over the outer loop variable. 4. Evaluate the summation or multiplication, then drop lower-order terms and constant factors to yield the final O()O() notation.

Checking the result

Select small integer values for nn and trace the loops to count the exact number of innermost executions. Verify that the polynomial degree of the exact count matches the calculated asymptotic complexity.

Common errors

A frequent error is assuming any two nested loops automatically result in O(n2)O(n^2) complexity. Another common mistake is ignoring step sizes, such as a variable doubling (i=iimes2i = i imes 2), which yields logarithmic O(logn)O(\log n) iterations rather than linear ones.

Worked example

Find the Big O time complexity of a loop where the outer loop variable ii goes from 1 to nn, and the inner loop variable jj goes from 1 to ii, with both incrementing by 1.

The outer loop runs nn times. For each iteration ii, the inner loop runs exactly ii times. The total number of operations is expressed by the summation: i=1ni=n(n+1)2=n22+n2\sum_{i=1}^{n} i = \frac{n(n+1)}{2} = \frac{n^2}{2} + \frac{n}{2}. Dropping the constant divisor and the lower-order linear term n2\frac{n}{2}, the asymptotic time complexity is O(n2)O(n^2).

FAQ

Run your own problem

References: Introduction to Algorithms (Cormen, Leiserson, Rivest, Stein) · The Algorithm Design Manual (Skiena)

See also