What is Time complexity (Big-O notation)?
A description of how the work (time complexity) or memory (space complexity) an algorithm needs grows with the number of data items n. It is written in Big-O notation, such as O(n), O(n log n) or O(n²), dropping constant factors and lower-order terms.
Japanese name: 計算量
Time complexity (Big-O notation) appears in 2 practice questions across 1 topic, including Data Structures and Algorithms.
Field: Data Structures and Algorithms
Reading Big-O notation
O(f(n)) means that for sufficiently large n the work stays within a constant multiple of f(n). An algorithm that makes 3n² + 5n + 2 comparisons is written O(n²), because the n² term dominates as n grows. Common classes, in increasing order, are O(1) (constant), O(log n) (binary search), O(n) (linear search), O(n log n) (merge sort, heapsort), O(n²) (bubble sort, selection sort) and O(2ⁿ) (naive exhaustive search). For n = 1000, log₂ n is about 10, n log₂ n about 10,000 and n² one million, so for large data the difference in complexity is decisive.
Best, average and worst cases
The same algorithm can do different amounts of work depending on the input. Insertion sort makes n−1 comparisons, O(n), on sorted data, but n(n−1)/2, O(n²), on reversed data. Quicksort is O(n log n) on average but O(n²) in the worst case, depending on the pivot choice and the data. Systems that need a bound on response time care about the worst case, while estimates of total throughput use the average case. When an occasional expensive step averages out to a constant over many operations, as with appending to a dynamic array, the cost is described as amortized O(1).
Real speed is not decided by complexity alone
Because Big-O notation ignores constant factors, two O(n) algorithms can differ greatly in real speed. On modern computers, main memory is nearly 100 times slower than the L1 cache, so reading an array in order is many times faster than walking a linked list. For small n, a simple algorithm with a worse complexity can also be faster, which is why practical sort implementations use insertion sort for short ranges. When improving performance, first check that no algorithm with an unnecessarily high complexity is being used, then measure and refine details such as memory access patterns.
FAQ
What is the base of the logarithm in O(log n)?
Changing the base only changes the value by a constant factor (log₂ n = log₁₀ n ÷ log₁₀ 2), so Big-O notation usually omits it. For algorithms that halve the data each step, such as binary search, the actual count is close to log₂ n.
What is the difference between time complexity and space complexity?
Time complexity is how the work (running time) grows, and space complexity is how the required memory grows. Both are compared together: merge sort is O(n log n) in time with O(n) working space, while heapsort is O(n log n) in time with O(1) space.