Time complexity describes how the number of steps an algorithm takes grows as its input grows. You find it by counting how many times the busiest line runs, then keeping only the part that grows fastest. This lesson shows each common pattern with a loop you can run and count yourself.
In this lesson you will learn
- What time complexity measures, and why it is not the same as seconds
- The loop shape behind O(1), O(log n), O(n), O(n log n) and O(n²)
- Why naive recursion can explode to O(2n)
- Four rules for finding the Big O of any piece of code
- How to tell, before you run it, whether code is fast enough
Steps, Not Seconds
Imagine looking for a friend’s number in your phone’s contacts. If you scroll from the top, the time grows with the size of your contact list. If you type the first letters of the name, the search jumps almost straight there, however many contacts you have. Neither approach is about how fast your phone is. It is about how the work grows as the list gets longer.
That growth is what time complexity captures. We count basic steps as a function of the input size n, then describe the growth with Big O notation. The same code has the same time complexity on a cheap phone and on a server.
Five Loop Patterns, Counted
Each function below counts how many times its innermost line runs. Read the code, guess the counts, then compare with the output.
#include <stdio.h>
/* Count how many times the innermost line runs for each pattern. */
static long constant(long n) { (void)n; return 1; }
static long linear(long n) { long c = 0; for (long i = 0; i < n; i++) c++; return c; }
static long logn(long n) { long c = 0; for (long i = 1; i < n; i *= 2) c++; return c; }
static long nlogn(long n) { long c = 0; for (long i = 0; i < n; i++) for (long j = 1; j < n; j *= 2) c++; return c; }
static long quadratic(long n) { long c = 0; for (long i = 0; i < n; i++) for (long j = 0; j < n; j++) c++; return c; }
int main(void) {
long ns[] = {8, 64, 1024};
printf("%6s %6s %6s %8s %10s %10s\n", "n", "O(1)", "log n", "n", "n log n", "n^2");
for (int k = 0; k < 3; k++) {
long n = ns[k];
printf("%6ld %6ld %6ld %8ld %10ld %10ld\n",
n, constant(n), logn(n), linear(n), nlogn(n), quadratic(n));
}
return 0;
}
Output
n O(1) log n n n log n n^2
8 1 3 8 24 64
64 1 6 64 384 4096
1024 1 10 1024 10240 1048576
O(1): Constant
The work does not depend on n at all: reading arr[5], adding two numbers, checking whether a number is even. Whether n is 8 or 8 billion, it is one step.
O(log n): The Counter Doubles
The loop for (i = 1; i < n; i *= 2) doubles i each time, so it reaches n after about log₂n rounds. Going from n = 64 to n = 1,024 (16 times bigger) adds only 4 steps. Binary search works this way, halving the search area each step.
O(n): One Pass
A single loop over the input. Double the input and the work doubles. Finding the largest number, summing a list and linear search are all O(n).
O(n log n): A Halving Loop Inside a Full Loop
An O(log n) loop run n times gives n × log n. This is the cost of the best general sorting algorithms, such as merge sort and heap sort.
O(n²): A Loop Inside a Loop
For each of n items, look at all n items again: n × n steps. Make n 16 times bigger (64 → 1,024) and the work grows 256 times (4,096 → 1,048,576). Checking every pair of items, and simple sorts like bubble sort, are O(n²).
A Loop That Shrinks Is Still O(n²)
Many pair-checking loops start the inner loop at i instead of 0, so they do less work. How much less?
#include <stdio.h>
/* A triangle loop: j starts at i, so the inner loop shrinks each time. */
int main(void) {
for (int n = 10; n <= 1000; n *= 10) {
long count = 0;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
count++;
printf("n = %4d: %7ld steps (n*n/2 = %ld)\n", n, count, (long)n * n / 2);
}
return 0;
}
Output
n = 10: 55 steps (n*n/2 = 50)
n = 100: 5050 steps (n*n/2 = 5000)
n = 1000: 500500 steps (n*n/2 = 500000)
The count is n(n + 1)/2, which is about n²/2. That is half of a full n × n loop, but halving is only a constant factor. The growth is still quadratic: ten times the input gives about a hundred times the work. So the triangle loop is O(n²).
O(2n): When Recursion Branches
The naive way to compute Fibonacci numbers calls itself twice for every value. The number of calls grows by a fixed factor each time n goes up, not by a fixed amount.
#include <stdio.h>
static long calls;
static long fib(int n) { /* naive recursion: O(2^n) */
calls++;
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
int main(void) {
for (int n = 10; n <= 30; n += 5) {
calls = 0;
long f = fib(n);
printf("fib(%d) = %6ld needed %8ld calls\n", n, f, calls);
}
return 0;
}
Output
fib(10) = 55 needed 177 calls
fib(15) = 610 needed 1973 calls
fib(20) = 6765 needed 21891 calls
fib(25) = 75025 needed 242785 calls
fib(30) = 832040 needed 2692537 calls
Each increase of 5 in n multiplies the calls by about 11. By fib(50) this code would make tens of billions of calls. The fix is to remember answers you have already computed (memoisation) or to use a simple loop. Both make it O(n).
Four Rules for Finding the Big O
- Steps one after another: add, then keep the biggest. An O(n) loop followed by an O(n²) loop is O(n + n²) = O(n²).
- Loops inside loops: multiply. An O(n) loop containing an O(log n) loop is O(n log n).
- Drop constant factors. 3n, n/2 and n + 100 are all O(n).
- Watch how the loop variable changes.
i++ori += 2means about n rounds.i *= 2ori /= 2means about log n rounds.
One more check: look for hidden loops inside library calls. strlen(s) walks the whole string, so writing for (i = 0; i < strlen(s); i++) turns an O(n) loop into O(n²). Store the length in a variable first.
Is My Code Fast Enough?
A rough rule from competitive programming: a modern computer does about 100 million simple steps per second. Use it to estimate before you code.
| Complexity | Largest n that runs in about 1 second |
|---|---|
| O(n) | about 100 million |
| O(n log n) | about 5 million |
| O(n²) | about 10,000 |
| O(n³) | about 450 |
| O(2n) | about 26 |
If a problem says n can be 1,000,000, an O(n²) idea is too slow before you write a single line. These are estimates, not exact limits; real speed depends on the work in each step.
Common Mistakes
- Timing instead of counting. Run times vary with load and hardware; step counts do not.
- Calling every nested loop O(n²). If the inner loop doubles its counter, the pair is O(n log n).
- Keeping constants. O(n²/2) is just O(n²).
- Testing only small inputs. An O(n²) program can look instant at n = 100 and take hours at n = 1,000,000.
- Forgetting the cost of function calls. A loop that calls an O(n) function n times is O(n²).
Practice Exercises
Exercise 1. Find the time complexity.
for (int i = 0; i < n; i++) sum += a[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < 10; j++) sum += a[i] * j;
Show answer
The first loop is O(n). The second is O(10n) = O(n), because the inner loop always runs 10 times. In total: O(n).
Exercise 2. Find the time complexity.
for (int i = n; i > 1; i /= 2)
for (int j = 0; j < n; j++) count++;
Show answer
The outer loop halves i, so it runs about log n times. The inner loop runs n times each. Total: O(n log n).
Exercise 3. A program must handle n = 200,000. Is an O(n²) algorithm acceptable?
Show answer
No. 200,000² = 40 billion steps, roughly 400 seconds at 100 million steps per second. You need O(n log n) or better.
Frequently Asked Questions
What is the difference between time complexity and running time?
Running time is measured in seconds on one machine. Time complexity describes how the number of steps grows with input size, independent of the machine.
Is O(log n) faster than O(n)?
For large inputs, yes, by a huge margin. For a million items, log₂n is about 20 while n is a million.
Why is a nested loop sometimes not O(n²)?
When one of the two loops does not run about n times. If a loop runs a fixed number of times, or halves or doubles its counter, the pair is O(n) or O(n log n) instead.
Should I learn space complexity too?
Yes. Memory is the other main cost of an algorithm. See our Space Complexity lesson.
Summary
- Time complexity counts steps as a function of n, not seconds.
- One loop is O(n); a doubling loop is O(log n); a loop in a loop is O(n²).
- Branching recursion like naive Fibonacci is O(2n).
- Add sequential parts, multiply nested parts, drop constants.
- Use about 100 million steps per second to estimate whether code is fast enough.
