Big-O Notation
| Concept | |
|---|---|
![]() Image placeholderPlace the file shown below in the assets/articles folder big-o-notation.webp | |
| Introduced | 1894 (Paul Bachmann) |
| Related | Omega, Theta, little-o |
| Field | Analysis of algorithms |
Big-O notation is a mathematical notation that describes the upper bound of how a function grows as its input becomes large. In computer science it is the standard language for stating how the running time or memory use of an algorithm scales with the size of its input, which is usually written as n.[2]
An algorithm described as O(n) takes time at most proportional to n for large inputs, while one described as O(n²) takes time at most proportional to the square of n. The notation deliberately ignores constant factors and lower-order terms, so that algorithms can be compared by their growth rate rather than by the speed of a particular machine.
History
The notation was introduced by the German mathematician Paul Bachmann in 1894 in his work on analytic number theory. Edmund Landau popularised it in the following decades, which is why the family of symbols is sometimes called Landau notation. In 1976 Donald Knuth proposed a consistent use of the related symbols Omega and Theta for computer science, clarifying the difference between upper, lower and tight bounds.[1]
Formal definition
A function f(n) is O(g(n)) if there are positive constants c and n₀ such that f(n) ≤ c·g(n) for every n ≥ n₀. In words: beyond some starting point, f never grows faster than a constant multiple of g. Because only the long-run behaviour matters, 3n² + 10n + 7 is simply O(n²). The leading term dominates, and its coefficient is absorbed into the constant c.
A statement such as "this loop is O(n²)" is an upper bound, not an exact count. An O(n) algorithm is therefore also, formally, O(n²), although that statement would be needlessly weak. When a bound is tight, the Theta symbol is used.
Related notations
| Symbol | Meaning | Informal reading |
|---|---|---|
| O(g) | Upper bound | grows no faster than g |
| Ω(g) | Lower bound | grows at least as fast as g |
| Θ(g) | Tight bound | grows at the same rate as g |
| o(g) | Strict upper bound | grows strictly slower than g |
Common growth classes
The classes below appear constantly in the analysis of algorithms. The examples show a typical operation for each.
| Class | Name | Example |
|---|---|---|
| O(1) | Constant | Reading an array element by index |
| O(log n) | Logarithmic | Binary search in a sorted array |
| O(n) | Linear | Scanning a list for a value |
| O(n log n) | Linearithmic | Merge sort, heap sort |
| O(n²) | Quadratic | Comparing every pair, simple bubble sort |
| O(2ⁿ) | Exponential | Trying every subset of a set |
| O(n!) | Factorial | Trying every ordering of n items |
The difference between these classes is dramatic. Doubling the input of a linear algorithm doubles the work; doubling the input of a quadratic algorithm quadruples it; adding a single element to an exponential algorithm doubles its running time. An O(n log n) sort of a million items needs roughly twenty million basic steps, while an O(n²) sort needs about a trillion.
How to analyse an algorithm
Analysis usually proceeds by counting the dominant operations.
- A single loop over n items is O(n).
- Two nested loops over n items are O(n²).
- A loop that halves the remaining input on each step is O(log n).
- Sequential blocks add, so O(n) followed by O(n²) is O(n²).
- A recursive algorithm gives a recurrence relation, which can be solved with the substitution method or the Master theorem.
Best, worst and average cases
Big-O says nothing by itself about which input is being measured. The same algorithm can have different bounds for different inputs. Quicksort, for instance, runs in O(n log n) on average but O(n²) in its worst case, when pivots are chosen badly. Hash tables offer O(1) lookups on average but O(n) in the worst case if many keys collide.[3] Stating which case is meant avoids confusion.
Amortised analysis
Some operations are occasionally expensive but cheap on average over a sequence. Appending to a dynamic array normally takes constant time, but when the array is full it must be copied to a larger block. Because the capacity grows geometrically, the total cost of n appends is O(n), so each append is O(1) amortised.
Space complexity
The same notation describes memory use. Merge sort needs O(n) extra space, while heap sort sorts in place with O(1) extra space. Recursion depth also counts, since each call occupies stack space.
Practical limits
Big-O describes growth, not speed. An algorithm with a smaller exponent can still lose to a simpler one on small inputs, because constant factors, memory locality and caching often dominate. Insertion sort, which is O(n²), is commonly used inside faster sorting routines for very short arrays for exactly this reason. Real performance work therefore combines complexity analysis with measurement on realistic data.
Complexity also interacts with hardware. An algorithm that walks through memory in order can run many times faster than one that jumps around, even when both have the same Big-O class, because of how processor caches work.
Common misconceptions
Big-O is often described as "the worst case", but the two ideas are independent: a bound can be given for the best, average or worst case alike. It is also not a measure of seconds; it cannot tell you how long a specific program takes. And it does not mean that lower is always better in practice, as shown by the cache and constant-factor effects above.
Even so, the notation remains the most useful shared vocabulary for discussing scalability. Choosing a data structure with a logarithmic lookup instead of a linear scan is often the single change that lets a program handle a hundred times more data.
Editorial connections and engineering implications
Asymptotic analysis and real machines
Big-O describes how resource use grows as input size becomes large; it intentionally ignores constants and lower-order terms. An O(n) implementation can still be slower than an O(n log n) implementation for small inputs if its constant overhead is higher. Conversely, memory locality, vectorised instructions and caching can make an algorithm with a less favourable theoretical bound perform well on a specific workload.
The input size must be defined carefully. A graph algorithm may depend on both vertices and edges, while a database operation may depend on row count, index structure and data distribution. Space analysis should include recursion depth, temporary buffers and retained runtime objects. Complexity notation is a model, not a benchmark: engineers should pair it with profiling of latency, throughput and memory use. This is particularly important for database queries, runtime code and distributed services, where I/O and contention can dominate computation.
See also
- Neural Network – A machine-learning model made from connected computational units that learn patterns by adjusting numerical parameters from data.
- CAP Theorem – A result stating that a distributed data store cannot simultaneously guarantee consistency, availability and partition tolerance.
- Event Loop – A programming construct that waits for and dispatches events or messages in a single thread of control.
- Garbage Collection – Automatic memory management in which a runtime reclaims memory that a program can no longer reach.
- Kubernetes – An open-source system for automating deployment, scaling and management of containerised applications.
- PostgreSQL – An open-source object-relational database system with a strong emphasis on standards compliance and extensibility.
- Python – A high-level, dynamically typed programming language that emphasises readability.
- Race Condition – A defect in which the outcome of a program depends on the unpredictable timing of concurrent operations.
References
- ↑ Knuth (1976), Big omicron and big omega and big theta (doi.org)
- ↑ Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th edition (MIT Press) (mitpress.mit.edu)
- ↑ Python documentation: TimeComplexity of built-in types (wiki.python.org)
