Asymptotic notations
Asymptotic notations describe how an algorithm's time or space grows as input size (n) increases. Big O gives an upper bound, Big Omega a lower bound, and Big Theta a tigh
Theory & Explanation
Asymptotic Notations
When we analyze an algorithm, we express its running time or space requirement as a mathematical function f(n), where n is the input size. However, exact functions like 3n2 + 5n + 2 contain constant factors and lower-order terms that make comparing two algorithms unnecessarily complicated.
Asymptotic notations are mathematical tools used to describe the running time or space complexity of an algorithm when the input size n becomes very large (approaches infinity). Instead of counting exact machine steps, asymptotic notations focus on the order of growth—the dominant term that decides how fast resource usage increases as data grows.
Why Do We Use Asymptotic Notations?
- Machine Independence: They allow us to compare algorithms without worrying about CPU speed, compiler differences, or programming language overhead.
- Focus on Scalability: For small values of n, almost every algorithm runs quickly. Asymptotic analysis reveals which algorithm remains practical when n grows to lakhs or crores of records.
- Simplicity: By ignoring constant multipliers and smaller terms (for example, simplifying
4n2 + 7n + 10ton2), we get a clean, standardized way to classify algorithms.
Types of Asymptotic Notations
There are three primary asymptotic notations used in algorithm analysis: Big O (O), Big Omega (Ω), and Big Theta (Θ).
| Notation | Symbol | Meaning (Bound) | Typical Use in Analysis |
|---|---|---|---|
| Big O Notation | O(g(n)) | Asymptotic Upper Bound (At most) | Worst-case time/space complexity |
| Big Omega Notation | Ω(g(n)) | Asymptotic Lower Bound (At least) | Best-case time/space complexity |
| Big Theta Notation | Θ(g(n)) | Asymptotic Tight Bound (Exact order of growth) | Average-case or when upper and lower bounds match |
1. Big O Notation — O(g(n)) [Upper Bound]
Definition: Big O notation defines the upper bound of an algorithm's growth rate. It tells us the maximum time (or space) an algorithm can take for a large input size n. In simple words, the algorithm will never perform worse than this rate.
Mathematical Expression: A function f(n) is said to be O(g(n)) if there exist positive constants c > 0 and n0 ≥ 1 such that:
0 ≤ f(n) ≤ c · g(n) for all n ≥ n0
Step-by-Step Example:
Suppose an algorithm has a step count function f(n) = 3n + 5. We want to prove that f(n) = O(n).
We need to find constants c and n0 such that 3n + 5 ≤ c · n for all n ≥ n0.
For all n ≥ 1, we know that 5 ≤ 5n.
Therefore: 3n + 5 ≤ 3n + 5n = 8n.
Here, choosing c = 8 and n0 = 1 satisfies f(n) ≤ c · g(n). Hence, 3n + 5 = O(n).
Practical Meaning: In Linear Search, if the target element is at the very last index or missing from an array of size n, the loop checks all n elements. Thus, its worst-case time complexity is O(n).
2. Big Omega Notation — Ω(g(n)) [Lower Bound]
Definition: Big Omega notation defines the lower bound of an algorithm's growth rate. It tells us the minimum time (or space) an algorithm will take for a large input size n. In simple words, the algorithm will take at least this much time and cannot run faster than this bound.
Mathematical Expression: A function f(n) is said to be Ω(g(n)) if there exist positive constants c > 0 and n0 ≥ 1 such that:
0 ≤ c · g(n) ≤ f(n) for all n ≥ n0
Step-by-Step Example:
Let f(n) = 3n + 5. We want to show that f(n) = Ω(n).
For all n ≥ 1, it is always true that 3n + 5 ≥ 3n (since 5 is positive).
Choosing c = 3 and n0 = 1 satisfies c · g(n) ≤ f(n). Hence, 3n + 5 = Ω(n).
Practical Meaning: In Linear Search, even in the luckiest scenario where the target element is at the first position, at least 1 comparison is required—giving a best-case complexity of Ω(1).
3. Big Theta Notation — Θ(g(n)) [Tight Bound]
Definition: Big Theta notation defines a tight bound by sandwiching the function f(n) between both an upper bound and a lower bound of the same order g(n). When an algorithm has the same upper and lower bound, Big Theta gives its exact asymptotic growth rate.
Mathematical Expression: A function f(n) is said to be Θ(g(n)) if there exist positive constants c1 > 0, c2 > 0, and n0 ≥ 1 such that:
0 ≤ c1 · g(n) ≤ f(n) ≤ c2 · g(n) for all n ≥ n0
Step-by-Step Example:
Consider f(n) = 3n + 5.
From our previous two proofs, for all n ≥ 1:
3n ≤ 3n + 5 ≤ 8n
Here, c1 = 3, c2 = 8, and n0 = 1. Since f(n) is bounded both from below and above by constant multiples of n, we write 3n + 5 = Θ(n).
Practical Meaning: Finding the sum of all elements in an array of size n always visits every single element once—no fewer and no more. Therefore, its time complexity is strictly Θ(n).
Rules for Simplifying Asymptotic Expressions
- Drop Constant Multipliers: Constants do not change the growth class. For example,
O(5n),O(100n), andO(n / 2)all simplify to O(n). - Drop Lower-Order Terms: Keep only the fastest-growing term as n becomes large. For example,
4n3 + 20n2 + 9n + 15simplifies to O(n3). - Base of Logarithm Does Not Matter: Changing the base of a logarithm only multiplies it by a constant factor, so
O(log2 n)andO(log10 n)are both written simply as O(log n).
Common Time Complexities Ranked (Fastest to Slowest)
When comparing algorithms for large inputs, the standard growth rates rank from most efficient to least efficient as follows:
O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n) < O(n!)
- O(1) — Constant Time: Execution time stays the same regardless of input size (e.g., accessing an array element by index
arr[i]). - O(log n) — Logarithmic Time: Input size is cut by a constant fraction at every step (e.g., Binary Search on a sorted array).
- O(n) — Linear Time: Work grows directly in proportion to input size (e.g., traversing an array or Linear Search).
- O(n log n) — Linearithmic Time: Common in efficient divide-and-conquer sorting algorithms (e.g., Merge Sort, Heap Sort).
- O(n2) — Quadratic Time: Typically occurs with two nested loops over the input (e.g., Bubble Sort, Selection Sort, Insertion Sort).
- O(2n) — Exponential Time: Operations double with each additional input element (e.g., naive recursive Fibonacci, generating all subsets).
- O(n!) — Factorial Time: Grows explosively even for small n (e.g., generating all permutations of a string or brute-force Travelling Salesperson Problem).
Program Implementations
#include <stdio.h>
int main(void) {
int sizes[] = {5, 10, 20};
int num_sizes = 3;
printf("n\tLinear Ops (n)\tQuadratic Ops (n*n)\n");
printf("--------------------------------------------\n");
for (int idx = 0; idx < num_sizes; idx++) {
int n = sizes[idx];
long long linear_ops = 0;
long long quad_ops = 0;
/* Single loop: runs n times -> O(n) */
for (int i = 0; i < n; i++) {
linear_ops++;
}
/* Nested loops: runs n * n times -> O(n^2) */
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
quad_ops++;
}
}
printf("%d\t%lld\t\t%lld\n", n, linear_ops, quad_ops);
}
return 0;
}
n Linear Ops (n) Quadratic Ops (n*n) -------------------------------------------- 5 5 25 10 10 100 20 20 400
