Big O Notation
Also known as: Asymptotic notation, Landau notation
Mathematical notation describing how an algorithm's running time or memory use grows as its input size grows, ignoring constant factors.
Big O notation expresses an upper bound on the growth rate of a function. In computing it describes how the cost of an algorithm scales with the size of its input, usually written n. An algorithm that is O(n) does work roughly proportional to the input, so doubling the input roughly doubles the time. One that is O(n^2), such as a naive method that compares every pair of items, takes about four times as long when the input doubles. O(1) means the cost stays bounded regardless of size, and O(log n) grows very slowly.
The notation came from mathematics. The German number theorist Paul Bachmann introduced the O symbol in 1894, and Edmund Landau popularized it, which is why the family is sometimes called Landau notation. Donald Knuth promoted its systematic use in computer science and, in a 1976 article, advocated the related symbols big Omega for lower bounds and big Theta for tight bounds. Analysis typically ignores constant factors and lower-order terms, because for large inputs the fastest-growing term determines how an algorithm behaves.
Big O analysis helps programmers predict whether code will cope with growth: an O(n^2) routine that is fine for a hundred records can become unusable for a million. It also guides the choice of data structures, since a hash table lookup takes O(1) time on average while scanning a list is O(n). The notation has limits. It hides constant factors that can dominate in practice, describes behavior only as inputs grow large and is often used loosely for average rather than worst-case cost, so real performance still needs to be measured.
Key points
- Big O describes an upper bound on growth, letting algorithms be compared independently of hardware speed.
- Common classes include O(1), O(log n), O(n), O(n log n) and O(n^2), each growing faster than the last.
- Paul Bachmann introduced the notation in 1894, and Donald Knuth championed its use in computer science.
- Constant factors and small inputs are ignored, so profiling remains necessary to understand actual speed.
Related entries
Algorithm
Finite sequence of well-defined steps for solving a problem or performing a computation, such as sorting a list or finding a route.
Data Structure
Way of organizing and storing data in a computer so that it can be accessed and modified efficiently, such as an array, list, tree or hash table.
Hash Table
Data structure that stores key-value pairs and uses a hash function to compute where each item lives, giving very fast average lookups.
Donald Knuth
American computer scientist who wrote "The Art of Computer Programming", created the TeX typesetting system and shaped the analysis of algorithms.
Dynamic Programming
Algorithmic technique that solves complex problems by breaking them into overlapping subproblems and storing each result so it is computed only once.
Recursion
Technique in which a function solves a problem by calling itself on smaller instances of the same problem until it reaches a simple base case.
Tree (Data Structure)
Hierarchical data structure of nodes connected by edges, with a single root and no cycles, used for file systems, indexes and parsers.