Time complexity is how the running time grows with input size n. Space complexity is how much extra memory is used. We usually give the worst case with Big-O.
- Big-O (O) — upper bound (worst case).
- Big-Omega (Ω) — lower bound (best case).
- Big-Theta (Θ) — tight bound (both).
O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ)
x = a[5]; // O(1)
for (int i = 1; i < n; i *= 2) {} // O(log n)
for (int i = 0; i < n; i++) {} // O(n)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {} // O(n^2)
sort(a, a + n); // O(n log n)
Rule of thumb: about 108 simple operations run in ~1 second. For n = 105, O(n²) is too slow but O(n log n) is fine.