How to Find Big-O Time Complexity by Counting Loops and Recursion
Work out Big-O by counting loops: nested loops multiply, sequential loops add, halving gives log n, plus recurrences and real running time estimates.

How to Find Big-O Time Complexity by Counting Loops and Recursion
Big-O notation describes how the work an algorithm does grows as its input grows. It ignores constant factors and small terms on purpose, because for large inputs the shape of the growth matters far more than the details. In practice, most questions come down to looking at loops and recursive calls and counting how many times the innermost line runs.
The core rules
- A loop that runs n times costs n times whatever is inside it.
- Nested loops multiply: a loop of n inside a loop of n is n·n = n^2.
- Loops one after another add, and you keep only the largest term: n + n^2 is O(n^2).
- A counter that doubles, or a range that halves, takes about log2 n steps, so it is O(log n).
- A loop with a fixed number of passes, such as exactly 10, is O(1).
- Drop constants: 3n + 7 is O(n), and n/2 is O(n).
Worked example 1: a triangle loop plus a halving loop
for i in 1..n:
for j in 1..i:
count += 1
k = n
while k > 1:
k = k // 2
The inner loop runs 1 time when i = 1, 2 times when i = 2 and so on up to n. The total is 1 + 2 + ... + n = n(n + 1)/2. For n = 1000 that is 500,500 increments. Even though the inner loop is shorter on average, the total is still about n^2/2, which is O(n^2).
The while loop halves k each pass, so it runs about log2 n times, roughly 10 times for n = 1000. That is tiny next to half a million.
Adding the two parts gives n(n + 1)/2 + log2 n, and keeping the dominant term leaves O(n^2).
Worked example 2: a loop with a tripling counter inside
for i in 1..n:
m = 1
while m < n:
m = m * 3
The inner loop multiplies by 3 until it reaches n, which takes about log3 n steps. The outer loop repeats it n times, so the total is n log n. The base of the logarithm does not matter in Big-O, because changing base only multiplies by a constant.
Recursion
For a recursive function, write the cost as a recurrence and recognise the pattern:
- T(n) = T(n - 1) + O(1): one call on a slightly smaller input, O(n).
- T(n) = T(n/2) + O(1): binary search, O(log n).
- T(n) = 2T(n/2) + O(n): merge sort, O(n log n).
- T(n) = 2T(n - 1) + O(1): two calls that each shrink by one, as in naive Fibonacci, O(2^n).
The last pattern is the dangerous one. The number of calls doubles at each level, so it explodes even for modest n.
Why it matters: real running times
Assume a machine performs about 10^8 simple steps per second and n = 100,000.
- An O(n log n) method takes about 100,000 × 16.6 ≈ 1.66 million steps, around 0.017 seconds.
- An O(n^2) method takes 10^10 steps, about 100 seconds.
Same task, same machine, a difference of almost four orders of magnitude. That is why choosing a better algorithm usually beats buying a faster computer.
Common mistakes
- Counting the number of loops instead of how they are arranged. Two separate loops over n are O(n), not O(n^2).
- Assuming a triangle loop is O(n log n) because the inner loop is "shorter".
- Missing hidden costs. Calling a function that searches a list inside a loop adds another factor of n.
- Keeping constants, such as writing O(2n) or O(n^2/2).
- Treating a loop that stops early in some cases as if it always does. Big-O is usually quoted for the worst case.
How to check an estimate
Count the operations for two input sizes, say n = 1000 and n = 2000. If the count roughly doubles, the growth is linear; if it roughly quadruples, it is quadratic; if it barely changes, it is logarithmic or constant. You can do this on paper with the formulas above or by adding a counter to your program.
Doing it with the tool
Paste either example above into the Big-O Complexity Explainer. It finds each loop, labels its cost as n, log n, square root of n or constant, follows the nesting and lists the reasoning line by line before giving the overall order. Then open the growth chart to compare n log n with n^2, switch on the logarithmic axis to see all the curves at once, and enter n = 100000 with 10^8 operations per second to reproduce the times above. The practice tab shows short snippets for you to classify.
Limits
The explainer reads the text of the code and matches patterns; it does not run your program, so it cannot see loop bounds that depend on data, early exits or the cost of library calls. Space complexity and amortised analysis are not covered. Treat its answer as a study aid and confirm the reasoning yourself, especially for unusual loops.


