Time complexity describes how the number of basic operations grows with input size n. A single loop is linear, nested loops over the same data are quadratic, and a loop that halves its range each step is logarithmic. You compare algorithms by these growth rates, not by raw seconds.
Remember
for (i = 0; i < n; i++) /* O(n) */
for (i...) for (j...) /* O(n^2) */
for (k = n; k > 1; k /= 2) /* O(log n) */
Reference
Cheat Sheet
Keep these facts handy while you work with Time Complexity in C.
Topic
Detail
O(1)
Constant
O(log n)
Halving
O(n)
One pass
O(n^2)
Nested passes
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Space Complexity.
Example 1 — Count steps for three loop shapes
Try n = 8 and n = 16.
C
#include <stdio.h>
int main(void) {
int sizes[] = {8, 16};
for (int s = 0; s < 2; s++) {
int n = sizes[s];
int linear = 0, quadratic = 0, logarithmic = 0;
for (int i = 0; i < n; i++) linear++;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) quadratic++;
for (int k = n; k > 1; k /= 2) logarithmic++;
printf("n=%d: linear=%d quadratic=%d log=%d\n", n, linear, quadratic, logarithmic);
}
return 0;
}
Doubling n doubles the linear count but quadruples the quadratic count.
The halving loop only gains one step when n doubles.
This is why O(log n) scales so well.
Edge Cases & Pitfalls
Check these before relying on Time Complexity in real programs.
hidden
Hidden loops
A library call like strlen inside a loop adds another factor of n.
drop
Dropping the dominant term
Keep the fastest-growing term: n^2 + n is O(n^2).
Remember
Key Takeaways
Count operations.
Nested loops multiply.
Halving gives log n.
One line: Time complexity describes how the number of basic operations grows with input size n.
Frequently Asked Questions
A measure of how the number of operations an algorithm performs grows as the input size grows.
Two loops over n items each are O(n^2); three are O(n^3).
Continue with "C Space Complexity" next. Compare constant extra space with stack-depth space in recursion. The C Algorithm Basics hub lists every topic in order.