Skip to main content

Big O

This is the nicest intro and should clear things up.

Big-O ComplexityName / Growth RateTypical Examples
O(1)O(1)ConstantArray indexing, hash table lookup, pushing/popping from a stack or queue
O(logn)O(log n)LogarithmicBinary search, tree search in balanced BST/AVL/Red-Black Tree, heap operations (insert/extract-min)
O(n)O(n)LinearLinear search, traversing an array or linked list, finding max/min in unsorted list
O(nlogn)O(n log n)Log-linearMerge sort, heap sort, quicksort (average case), efficient sorting in general
O(n2)O(n²)QuadraticBubble sort, insertion sort, selection sort, checking all pairs in a list
O(n3)O(n³)CubicNaïve matrix multiplication, checking all triplets
O(2n)O(2^n)ExponentialRecursive solution to Traveling Salesman Problem (TSP), generating all subsets, naïve Fibonacci recursion
O(n!)O(n!)FactorialSolving Traveling Salesman by brute force, generating all permutations

Operations on most Python data structures (e.g. appending a list, checking membership in a tuple) are O(1)O(1), O(log n)O(log\space n), or O(n)O(n).

Also note that if you do O(n)O(n) things mm times, your complexity is O(m×n)O(m \times n)