Big-O notation is a notation in mathematics that bounds the magnitude of a function by a constant multiple of another function as its argument approaches a specified limit. In computer science, it commonly describes how the running time or memory requirements of an algorithm increase with input size. It expresses an asymptotic upper bound, not necessarily an exact growth rate, and disregards constant factors and behavior outside the relevant limiting region. (xlinux.nist.gov)
Formal definition
For functions and defined on sufficiently large positive integers, with eventually, the statement
means that there exist constants and such that
The constants must not depend on . For nonnegative resource-counting functions, the absolute-value signs can be omitted. The threshold allows the inequality to fail for finitely many small inputs without invalidating the bound. (xlinux.nist.gov)
More generally, in mathematical analysis, as means that remains bounded near the relevant limit, wherever the ratio is defined. The argument may approach infinity, zero, or another point; it need not represent input size. (dlmf.nist.gov)
Strictly, denotes a set of functions satisfying the bound, so makes its meaning explicit. The conventional equals sign in does not express ordinary symmetric equality: one cannot reverse the statement and infer . (ocw.mit.edu)
Examples and calculation rules
Consider the polynomial
For , , establishing . It is also , demonstrating that a valid upper bound need not be the most informative one. Constant factors and lower-degree terms do not change the polynomial’s leading growth order. (xlinux.nist.gov)
For eventually nonnegative comparison functions, the definition gives useful rules:
- If and , then .
- Under the same assumptions, .
- If and , then .
- Multiplication by a fixed nonzero constant leaves the Big-O class unchanged. (cs.yale.edu)
These rules must be applied to actual operation counts. Nested loops do not automatically imply quadratic time: their iteration limits and the cost of their bodies determine the total work. (introcs.cs.princeton.edu)
Algorithm analysis and growth classes
Within computational complexity, Big-O notation describes resource usage relative to a stated input-size measure and computational model. Time complexity counts operations, while space complexity measures storage. The same data structure or algorithm may therefore have different time and space bounds. (xlinux.nist.gov)
Common bounds include the following, assuming constant-cost elementary operations:
| Bound | Conventional description | Example |
|---|---|---|
| Constant | Accessing an array element by index | |
| Logarithmic | [[binary-search | |
| Linear | Scanning all elements | |
| Linearithmic | [[merge-sort | |
| Quadratic | Examining every pair of elements | |
| Exponential | Enumerating every subset of elements |
Changing the fixed base of a logarithm greater than one changes only a constant factor, so it does not change these Big-O classes. (cs.princeton.edu)
Algorithms using recursion often lead to recurrence relations. For example, the divide-and-conquer structure of merge sort produces a recurrence with two half-sized subproblems and linear merging work, yielding an time bound. (introcs.cs.princeton.edu)
Big-O does not inherently mean “worst case.” A bound may describe worst-case, best-case, or average-case cost, provided the function being bounded is specified. An average-case claim requires assumptions about inputs; an expected bound for a randomized algorithm can instead average over its internal random choices. (algs4.cs.princeton.edu)
Related asymptotic notation
Several related symbols distinguish different kinds of comparison. For eventually positive functions:
- Big-Omega notation, , gives an asymptotic lower bound.
- Big-Theta notation, , gives both upper and lower bounds by positive constant multiples.
- Little-o notation, , means .
- Asymptotic equivalence, , means . (cs.yale.edu)
Thus is sharper than merely stating , while expresses strictly smaller growth. A positive finite limit of establishes a Theta bound, but a Big-O bound does not require that ratio to converge. (ocw.mit.edu)
Approximation errors and interpretive limits
In asymptotic expansions, Big-O can specify the magnitude of a remainder. For example,
means that the difference is bounded in magnitude by a constant times sufficiently near zero. Unlike a runtime bound at infinity, this describes an error shrinking toward zero. (dlmf.nist.gov)
An asymptotic runtime bound does not specify execution time in seconds. Constant factors, implementation details, and machine characteristics can affect practical performance; a smaller asymptotic bound need not imply faster execution at every finite input size. Input representation also matters: the number of stored items and the number of bits needed to encode them are different size measures. (introcs.cs.princeton.edu)