Input
n may count elements, vertices, bits or digits. State it explicitly.
Algorithms · Complexity analysis
Asymptotic notation describes how an algorithm’s cost grows as its input becomes large. It ignores machine details and constants, but not the structure of the problem.
Choose an input-size measure n and count a dominant operation: comparisons, accesses, additions or allocations. Time T(n) and space S(n) are functions of n, not exact seconds or megabytes.
n may count elements, vertices, bits or digits. State it explicitly.
Count how often a meaningful operation is executed.
Memory beyond the input itself, including the recursion stack.
| Symbol | Meaning | Definition | Reading |
|---|---|---|---|
| O(g(n)) | Asymptotic upper bound | 0 ≤ f(n) ≤ cg(n) | Grows no faster than g, up to a constant. |
| Ω(g(n)) | Asymptotic lower bound | 0 ≤ cg(n) ≤ f(n) | Grows at least as fast as g. |
| Θ(g(n)) | Tight bound | c₁g(n) ≤ f(n) ≤ c₂g(n) | Same growth order. |
| o(g(n)) | Strict upper bound | lim f(n)/g(n) = 0 | f grows strictly more slowly. |
| ω(g(n)) | Strict lower bound | lim f(n)/g(n) = ∞ | f grows strictly faster. |
In the first three definitions there are positive constants and a threshold n₀ beyond which the inequality holds. Θ(g(n)) equals O(g(n)) ∩ Ω(g(n)).
| Order | Name | Typical example | n = 1.000 |
|---|---|---|---|
| Θ(1) | constant | array access | 1 |
| Θ(log n) | logarithmic | binary search | ≈ 10 |
| Θ(n) | linear | scan | 1.000 |
| Θ(n log n) | linearithmic | merge sort | ≈ 10.000 |
| Θ(n²) | quadratic | nested loops | 1.000.000 |
| Θ(2ⁿ) | exponential | subset enumeration | impractical |
log n uses base 2. Changing the logarithm base only adds a constant factor, so the Θ class is unchanged.
Minimum cost among inputs of size n. Useful for adaptive algorithms.
Expected value under a stated probability model for the inputs.
Maximum cost for an input of size n; it provides a guarantee.
Average cost per operation over a sequence, without assuming random inputs.
for i = 0 … n - 1
visita A[i]n constant-cost iterations: Θ(n).
for i = 0 … n - 1
for j = i + 1 … n - 1n(n−1)/2 = Θ(n²).
while n > 1
n = n / 2After k steps n/2ᵏ ≤ 1, therefore k = Θ(log n).
8n³ + 2n² log n + 900
for (i = 1; i < n; i *= 3)
Is n log n = o(n²)?