ARTICLE · Analysis

Complexity cheat-sheet

Lower bound for comparison sorts: Omega(n log n). Counting/radix/bucket beat it by not comparing.

ArticleAnalysis© Arthur Bernier Jr1055 words

(a) Data structure operations (average / worst)#

StructureAccessSearchInsertDeleteSpaceNotes
Array (static)O(1)O(n)O(n)O(n)O(n)insert/delete = shift
Dynamic array (JS Array)O(1)O(n)O(1) amortized push; O(n) unshift/spliceO(1) pop; O(n) shift/spliceO(n)doubling growth
Singly linked listO(n)O(n)O(1) at head (with ref)O(1) at head (with ref)O(n)no random access
Doubly linked listO(n)O(n)O(1) with node refO(1) with node refO(n)LRU backbone
Hash map / set (Map, Set, object)—O(1) avg / O(n) worstO(1) avgO(1) avgO(n)worst = all collide
BST (balanced: AVL, red-black)O(log n)O(log n)O(log n)O(log n)O(n)in-order = sorted
BST (unbalanced)O(h) up to O(n)O(n)O(n)O(n)O(n)degenerates to list
Binary heapO(1) peekO(n)O(log n)O(log n) popO(n)heapify O(n)
Trie—O(L)O(L)O(L)O(alphabet * total chars)L = word length
Graph adjacency list—O(deg(v)) edge checkO(1)O(deg)O(V + E)sparse graphs
Graph adjacency matrixO(1) edge checkO(1)O(1)O(1)O(V^2)dense graphs; O(V) neighbors
Stack / queue (array / deque)O(1) topO(n)O(1)O(1)O(n)JS shift is O(n): use head index
Union-Find—O(alpha(n)) findO(alpha(n)) union—O(n)path compression + rank

(b) Sorting algorithms#

AlgorithmBestAverageWorstSpaceStableNotes
BubbleO(n)O(n^2)O(n^2)O(1)yesnever in practice
InsertionO(n)O(n^2)O(n^2)O(1)yessmall n, nearly sorted, online
SelectionO(n^2)O(n^2)O(n^2)O(1)nominimal swaps
MergeO(n log n)O(n log n)O(n log n)O(n)yeslinked lists, external sort
QuickO(n log n)O(n log n)O(n^2)O(log n) stacknorandom pivot; cache-friendly
HeapO(n log n)O(n log n)O(n log n)O(1)noguaranteed, in-place
CountingO(n + k)O(n + k)O(n + k)O(k)yessmall integer range k
RadixO(d(n + b))O(d(n + b))O(d(n + b))O(n + b)yesd digits, base b; fixed-width keys
BucketO(n + k)O(n + k)O(n^2)O(n)yesuniformly distributed floats
TimSort (JS Array.prototype.sort)O(n)O(n log n)O(n log n)O(n)yes (ES2019+)merge + insertion hybrid; runs

Lower bound for comparison sorts: Omega(n log n). Counting/radix/bucket beat it by not comparing.

(c) Common recurrences#

RecurrenceResolves toExample
T(n) = T(n/2) + O(1)O(log n)binary search
T(n) = T(n/2) + O(n)O(n)quickselect (avg), median of medians
T(n) = 2T(n/2) + O(1)O(n)tree traversal
T(n) = 2T(n/2) + O(n)O(n log n)merge sort, quick sort (avg)
T(n) = 2T(n/2) + O(n^2)O(n^2)—
T(n) = T(n-1) + O(1)O(n)linear recursion, factorial
T(n) = T(n-1) + O(n)O(n^2)quick sort worst, selection sort
T(n) = 2T(n-1) + O(1)O(2^n)naive fib, Tower of Hanoi, subsets
T(n) = n * T(n-1)O(n!)permutations
T(n) = T(n-1) + T(n-2)O(phi^n) ~ O(1.618^n)naive fibonacci

Master theorem, T(n) = aT(n/b) + O(n^d): compare d with log_b(a). d < log_b(a) -> O(n^(log_b a)); d = log_b(a) -> O(n^d log n); d > log_b(a) -> O(n^d).

(d) How to say it out loud#

TermMeaning (one line)
Amortizedaverage cost per op over a sequence; e.g. push is O(1) amortized though a single resize is O(n)
In-placeO(1) auxiliary space; mutates the input
Auxiliary spaceextra memory beyond the input (what "space complexity" usually means in interviews)
Stableequal keys keep their original relative order
Tight bound (Theta)both upper and lower bound match; "O" alone is just an upper bound
Worst case vs average casequick sort is O(n log n) average, O(n^2) worst
Best caseoften O(n) for adaptive sorts (insertion, TimSort) on sorted input
LinearithmicO(n log n)
Pseudo-polynomialpolynomial in the numeric VALUE of the input, not its bit length (knapsack O(nW))
Overlapping subproblemsthe same subproblem recurs -> memoize
Optimal substructureoptimal solution built from optimal sub-solutions -> DP / greedy
Greedy-choice propertya locally optimal choice is safe; prove with an exchange argument
Invariantproperty that holds before and after every loop iteration (say it for two pointers, sliding window, binary search)
Monotonicalways non-decreasing or non-increasing; enables binary search on the answer
Online algorithmprocesses input as it arrives (insertion sort, running median)
Divide and conquersplit, solve recursively, combine (merge sort)
Top-down vs bottom-upmemoized recursion vs iterative tabulation
Space-optimized / rolling arraykeep only the previous row(s) of the DP table
Alpha(n)inverse Ackermann; effectively constant (union-find)

See also