PublicSoftTools
Tools16 min read·PublicSoftTools Team·May 2026

Big O Cheat Sheet — Time and Space Complexity

Big O notation describes how an algorithm's time or space requirements scale as the input size grows. It is the universal language for comparing algorithm efficiency and a core topic in coding interviews. This guide covers all major complexity classes, data structure operation complexities, and common sorting algorithm complexities — with practical examples for each.

Big O Complexity Classes

NotationNameDescriptionExamplesRating
O(1)ConstantSame time regardless of input sizeArray access by index, hash table lookup (average), push/pop from stackBest
O(log n)LogarithmicTime grows very slowly -- each step halves the problemBinary search, balanced BST lookup, heap insert/deleteExcellent
O(n)LinearTime grows proportionally with input sizeLinear search, traversing a linked list, single-pass scanGood
O(n log n)LinearithmicSlightly worse than linear -- typical optimal sortingMerge sort, heap sort, quicksort (average), most efficient comparison sortsAcceptable
O(n^2)QuadraticTime grows with the square of input -- nested loopsBubble sort, insertion sort, selection sort, comparing all pairsPoor for large n
O(2^n)ExponentialTime doubles with each additional input elementNaive recursive Fibonacci, brute-force subset generation, travelling salesman (brute force)Avoid for n > 20
O(n!)FactorialTime grows as factorial of input -- all permutationsBrute-force travelling salesman (all routes), brute-force schedulingAvoid for n > 10

Data Structure Time Complexities

Data structureAccessSearchInsert (end)DeleteSpace
ArrayO(1)O(n)O(1) amortisedO(n)O(n)
Singly linked listO(n)O(n)O(1) with tail ptrO(n)O(n)
Hash tableN/AO(1) avg / O(n) worstO(1) avgO(1) avgO(n)
Binary search tree (balanced)O(log n)O(log n)O(log n)O(log n)O(n)
Heap (binary)Min/max: O(1)O(n)O(log n)O(log n)O(n)
Stack / QueueO(n)O(n)O(1)O(1)O(n)

What Big O Actually Means

Big O describes the worst-case upper bound of an algorithm's growth rate. Key properties:

Sorting Algorithm Complexities

The practical hierarchy for sorting:

Space Complexity

Space complexity measures how much additional memory an algorithm uses relative to input size. Types:

Space and time complexity often trade off. Hash tables use O(n) space to achieve O(1) lookup (instead of O(n) linear search with O(1) space). Dynamic programming uses O(n) or O(n^2) space to achieve polynomial-time solutions to problems that would otherwise be exponential.

Amortised Analysis

Some operations are occasionally expensive but cheap on average — amortised analysis averages the cost over many operations. The key example:

Dynamic array append: Appending to an array is O(1) most of the time. But when the array is full, it must reallocate and copy all elements — O(n). If the array doubles in size on each resize, resizes happen exponentially less often. The amortised cost per append is O(1) — the O(n) resizes are infrequent enough that when spread across all operations, each operation averages O(1).

This is why Python's list.append(), Java's ArrayList.add(), and JavaScript's Array.push() are all described as O(1) amortised, despite occasionally triggering O(n) resizes.

Practical Big O: When Does It Matter?

Big O matters most when:

Big O does not always determine which algorithm is faster in practice — constant factors matter for small n. A well-optimised O(n^2) algorithm can outperform a poorly-implemented O(n log n) one for small inputs. Profile first, then optimise.

Common Questions

What is the difference between O(log n) and O(n log n)?

O(log n) — logarithmic — is much faster than O(n log n) — linearithmic. For n = 1,000,000: O(log n) ≈ 20 operations; O(n log n) ≈ 20,000,000 operations. The difference is a factor of n. O(log n) is typical of binary search (halve the search space each step). O(n log n) is typical of efficient sorting (must examine all n elements, with each element taking O(log n) work on average).

What is the best time complexity for sorting?

For comparison-based sorting (algorithms that determine order by comparing elements), O(n log n) is the theoretical minimum — it can be proven that you cannot sort faster than this in the worst case with comparisons alone. Non-comparison sorts (counting sort, radix sort, bucket sort) can achieve O(n) for specific input types (integer keys within a bounded range), bypassing this lower bound.

What does O(1) space mean for an in-place algorithm?

O(1) (constant) space means the algorithm uses a fixed amount of extra memory that does not grow with input size — typically a handful of variables for swapping, counting, or tracking state. The input array itself is not counted in space complexity (it was already allocated). Heapsort and bubble sort are O(1) space. Merge sort is O(n) space because it needs an auxiliary array the same size as the input for the merge step. Recursion depth also counts as space — a recursive algorithm with depth n uses O(n) stack space even if it has no other allocation.

Visualise Algorithm Complexity

Use the Big O visualiser to compare how different complexities scale with input size — see the growth curves side by side.

Open Big O Visualiser