Big O Cheat Sheet

Complexity order: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ) < O(n!)

Array sorting algorithms

Algorithm Best Average Worst
Quick SortΩ(n log n)Θ(n log n)O(n²)
Merge SortΩ(n log n)Θ(n log n)O(n log n)
TimsortΩ(n)Θ(n log n)O(n log n)
Heap SortΩ(n log n)Θ(n log n)O(n log n)
Bubble SortΩ(n)Θ(n²)O(n²)
Insertion SortΩ(n)Θ(n²)O(n²)
Selection SortΩ(n²)Θ(n²)O(n²)
Tree SortΩ(n log n)Θ(n log n)O(n²)
Shell SortΩ(n log n)Θ(n(log n)²)O(n(log n)²)
Bucket SortΩ(n+k)Θ(n+k)O(n²)
Radix SortΩ(nk)Θ(nk)O(nk)
Counting SortΩ(n+k)Θ(n+k)O(n+k)
CubesortΩ(n)Θ(n log n)O(n log n)
Smooth SortΩ(n)Θ(n log n)O(n log n)
Tournament SortΘ(n log n)O(n log n)
Stooge SortO(nlog 3/log 1.5)
Gnome / Stupid SortΩ(n)Θ(n²)O(n²)
Comb SortΩ(n log n)Θ(n²/p²)O(n²)
Odd Even SortΩ(n)O(n²)

Data structures

S = Sorted, US = Unsorted. Binary heap heapify: O(n).

Same average and worst case

Structure Access Search Insertion Deletion
ArrayΘ(1)Θ(n)Θ(n)Θ(n)
StackΘ(n)Θ(n)Θ(1)Θ(1)
QueueΘ(n)Θ(n)Θ(1)Θ(1)
Singly Linked ListΘ(n)Θ(n)Θ(1)Θ(1)
Doubly Linked ListΘ(n)Θ(n)Θ(1)Θ(1)
B TreeΘ(log n)Θ(log n)Θ(log n)Θ(log n)
Red Black TreeΘ(log n)Θ(log n)Θ(log n)Θ(log n)
Splay TreeΘ(log n)Θ(log n)Θ(log n)
AVL TreeΘ(log n)Θ(log n)Θ(log n)Θ(log n)

Different average vs worst case

Structure Average Worst
AccessSearchInsertDelete AccessSearchInsertDelete
Skip ListΘ(log n)Θ(log n)Θ(log n)Θ(log n)O(n)O(n)O(n)O(n)
Binary Search TreeΘ(log n)Θ(log n)Θ(log n)Θ(log n)O(n)O(n)O(n)O(n)
KD TreeΘ(log n)Θ(log n)Θ(log n)Θ(log n)O(n)O(n)O(n)O(n)
Hash TableΘ(1)Θ(1)Θ(1)O(n)O(n)O(n)

Heap operations

Structure Find Max Extract Max Increase Key Insert Delete Merge
Linked List (S)O(1)O(1)O(n)O(n)O(1)O(m+n)
Linked List (US)O(n)O(n)O(1)O(1)O(1)O(1)
Binary HeapO(1)O(log n)O(log n)O(log n)O(log n)O(m+n)
Pairing HeapO(1)O(log n)O(log n)O(1)O(log n)O(1)
Binomial HeapO(1)O(log n)O(log n)O(1)O(log n)O(log n)
Fibonacci HeapO(1)O(log n)O(1)O(1)O(log n)O(1)

Graph data structure

Representation Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency listO(|V| + |E|)O(1)O(1)O(|V| + |E|)O(|E|)O(|V|)
Incidence listO(|V| + |E|)O(1)O(1)O(|E|)O(|E|)O(|E|)
Adjacency matrixO(|V|²)O(|V|²)O(1)O(|V|²)O(1)O(1)
Incidence matrixO(|V| · |E|)O(|V| · |E|)O(|V| · |E|)O(|V| · |E|)O(|V| · |E|)O(|E|)

Searching algorithms

AlgorithmTime
Linear SearchO(n)
Binary SearchO(log n)
Jump SearchO(√n)
Interpolation SearchO(log log n) best, O(n) worst
Exponential SearchO(log n)
Sequential SearchO(n)
Depth first search (DFS)O(|V| + |E|)
Breadth first search (BFS)O(|V| + |E|)

Graph algorithms

AlgorithmAverageWorst
DijkstraO(|E| log |V|)O(|V|²)
A* searchO(|E|)O(bd)
PrimO(|E| log |V|)O(|V|²)
Bellman FordO(|E| · |V|)O(|E| · |V|)
Floyd WarshallO(|V|³)O(|V|³)
Topological sortO(|V| + |E|)O(|V| + |E|)

Time complexity for Java collections

List

Collection Add Remove Get Contains Data structure
ArrayListO(1)O(n)O(1)O(n)Array
LinkedListO(1)O(1)O(n)O(n)Linked List
CopyOnWriteArrayListO(n)O(n)O(1)O(n)Array

List: Add is O(1) amortized for ArrayList. LinkedList Remove is O(1) at head/tail, O(n) by index.

Set

Collection Add Contains Next Data structure
HashSetO(1)O(1)O(h/n)Hash Table
LinkedHashSetO(1)O(1)O(1)Hash Table + Linked List
EnumSetO(1)O(1)O(1)Bit Vector
TreeSetO(log n)O(log n)O(log n)Red black tree
CopyOnWriteArraySetO(n)O(n)O(1)Array
ConcurrentSkipListSetO(log n)O(log n)O(1)Skip List

Queue

Collection Offer Peek Poll Size Data structure
PriorityQueueO(log n)O(1)O(log n)O(1)Priority Heap
LinkedListO(1)O(1)O(1)O(1)Linked List
ArrayDequeO(1)O(1)O(1)O(1)Array
ConcurrentLinkedQueueO(1)O(1)O(1)O(n)Linked List
ArrayBlockingQueueO(1)O(1)O(1)O(1)Array
PriorityBlockingQueueO(log n)O(1)O(log n)O(1)Priority Heap
SynchronousQueueO(1)O(1)O(1)O(1)None
DelayQueueO(log n)O(1)O(log n)O(1)Priority Heap
LinkedBlockingQueueO(1)O(1)O(1)O(1)Linked List

Map

Collection Get ContainsKey Next Data structure
HashMapO(1)O(1)O(h/n)Hash Table
LinkedHashMapO(1)O(1)O(1)Hash Table + Linked List
IdentityHashMapO(1)O(1)O(h/n)Array
WeakHashMapO(1)O(1)O(h/n)Hash Table
EnumMapO(1)O(1)O(1)Array
TreeMapO(log n)O(log n)O(log n)Red black tree
ConcurrentHashMapO(1)O(1)O(h/n)Hash Table
ConcurrentSkipListMapO(log n)O(log n)O(1)Skip List