Know Thy Complexities!
Hi there! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them.
Searching
Sorting
| Algorithm | Data Structure | Time Complexity | Worst Case Auxiliary Space Complexity |
|---|
| | Best | Average | Worst | Worst |
|---|
| Quicksort | Array | O(n log(n)) | O(n log(n)) | O(n^2) | O(n) |
| Mergesort | Array | O(n log(n)) | O(n log(n)) | O(n log(n)) | O(n) |
| Heapsort | Array | O(n log(n)) | O(n log(n)) | O(n log(n)) | O(1) |
| Bubble Sort | Array | O(n) | O(n^2) | O(n^2) | O(1) |
| Insertion Sort | Array | O(n) | O(n^2) | O(n^2) | O(1) |
| Select Sort | Array | O(n^2) | O(n^2) | O(n^2) | O(1) |
| Bucket Sort | Array | O(n+k) | O(n+k) | O(n^2) | O(nk) |
| Radix Sort | Array | O(nk) | O(nk) | O(nk) | O(n+k) |
Data Structures
| Data Structure | Time Complexity | Space Complexity |
|---|
| Average | Worst | Worst |
|---|
| Indexing | Search | Insertion | Deletion | Indexing | Search | Insertion | Deletion | |
|---|
| Basic Array | O(1) | O(n) | - | - | O(1) | O(n) | - | - | O(n) |
| Dynamic Array | O(1) | O(n) | O(n) | O(n) | O(1) | O(n) | O(n) | O(n) | O(n) |
| Singly-Linked List | O(n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(1) | O(n) |
| Doubly-Linked List | O(n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(1) | O(n) |
| Skip List | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) | O(n) | O(n) | O(n) | O(n log(n)) |
| Hash Table | - | O(1) | O(1) | O(1) | - | O(n) | O(n) | O(n) | O(n) |
| Binary Search Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) | O(n) | O(n) | O(n) | O(n) |
| Cartresian Tree | - | O(log(n)) | O(log(n)) | O(log(n)) | - | O(n) | O(n) | O(n) | O(n) |
| B-Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
| Red-Black Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
| Splay Tree | - | O(log(n)) | O(log(n)) | O(log(n)) | - | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
| AVL Tree | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) |
Heaps
Graphs
| Node / Edge Management | Storage | Add Vertex | Add Edge | Remove Vertex | Remove Edge | Query |
|---|
| Adjacency list | O(|V|+|E|) | O(1) | O(1) | O(|V| + |E|) | O(|E|) | O(|V|) |
| Incidence list | O(|V|+|E|) | O(1) | O(1) | O(|E|) | O(|E|) | O(|E|) |
| Adjacency matrix | O(|V|^2) | O(|V|^2) | O(1) | O(|V|^2) | O(1) | O(1) |
| Incidence matrix | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|V| ⋅ |E|) | O(|E|) |
Notation for asymptotic growth
| letter | bound | growth |
|---|
| (theta) Θ | upper and lower, tight[1] | equal[2] |
| (big-oh) O | upper, tightness unknown | less than or equal[3] |
| (small-oh) o | upper, not tight | less than |
| (big omega) Ω | lower, tightness unknown | greater than or equal |
| (small omega) ω | lower, not tight | greater than |
In short, if algorithm is __ then its performance is __| algorithm | performance |
|---|
| o(n) | < n |
| O(n) | ≤ n |
| Θ(n) | = n |
| Ω(n) | ≥ n |
| ω(n) | > n |