1 Θ, O, and Ω — Precise Definitions
The three standard asymptotic notations capture tight, upper, and lower bounds:
- O(g(n)) — upper bound: f(n) ≤ c·g(n) for all n ≥ n₀
- Ω(g(n)) — lower bound: f(n) ≥ c·g(n) for all n ≥ n₀
- Θ(g(n)) — tight bound: c₁·g(n) ≤ f(n) ≤ c₂·g(n) for all n ≥ n₀
f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) AND f(n) = Ω(g(n)). Always prefer Θ when you have a tight bound; say O only when an upper bound is all that is known.
Example: bubble sort comparisons = n(n-1)/2 Upper bound: O(n²) (since n(n-1)/2 ≤ n²) Lower bound: Ω(n²) (since n(n-1)/2 ≥ n²/4 for n ≥ 2) Tight: Θ(n²)
// Verify Theta(n^2) for bubble sort comparison count
function bubbleComparisons(n) {
return n * (n - 1) / 2;
}
for (const n of [10, 100, 1000]) {
const ratio = bubbleComparisons(n) / (n * n);
console.log(n, ratio.toFixed(3)); // converges to 0.5 — so Θ(n²)
}