Big-O Algorithm Complexity Cheat Sheet
Big-O Complexity Chart
!Pasted image 20240921162031.png
Common Data Structure Operations
| Data Structure |
Time Complexity |
|
|
|
|
|
|
|
Space Complexity |
|
Average |
|
|
|
Worst |
|
|
|
Worst |
|
Access |
Search |
Insertion |
Deletion |
Access |
Search |
Insertion |
Deletion |
|
| Array |
Θ(1) |
Θ(n) |
Θ(n) |
Θ(n) |
O(1) |
O(n) |
O(n) |
O(n) |
O(n) |
| Stack |
Θ(n) |
Θ(n) |
Θ(1) |
Θ(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
| Queue |
Θ(n) |
Θ(n) |
Θ(1) |
Θ(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
| Heap |
|
|
Θ(log(n)) |
Θ(log(n)) |
|
|
|
|
O(n) |
| Singly-Linked List |
Θ(n) |
Θ(n) |
Θ(1) |
Θ(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
| Doubly-Linked List |
Θ(n) |
Θ(n) |
Θ(1) |
Θ(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
| Skip List |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
O(n) |
O(n) |
O(n) |
O(n) |
O(n log(n)) |
| Hash Table |
N/A |
Θ(1) |
Θ(1) |
Θ(1) |
N/A |
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) |
O(n) |
| Cartesian Tree |
N/A |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
N/A |
O(n) |
O(n) |
O(n) |
O(n) |
| B-Tree |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(n) |
| Red-Black Tree |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(n) |
| Splay Tree |
N/A |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
N/A |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(n) |
| AVL Tree |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(log(n)) |
O(n) |
| KD Tree |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
Θ(log(n)) |
O(n) |
O(n) |
O(n) |
O(n) |
O(n) |
Arrays
Sorting Algorithms for Arrays
Best Sorting
- Merge sort has best/average/worst case Time complexity and space
- Quick sort has worst as but improves on space
- Python uses Timsort:
| Algorithm |
Time Complexity |
|
|
Space Complexity |
|
Best |
Average |
Worst |
Worst |
| Timsort |
Ω(n) |
Θ(n log(n)) |
O(n log(n)) |
O(n) |
Common patterns on Array
- Two pointers: , where is the work done at each iteration, includes sliding window
- Building a prefix sum:
- Finding the sum of a subarray given a prefix sum:
Strings
- Immutable, therefore any changes done creates a new string
|
Time Complexity |
| Insert |
|
| Access |
|
| Concatenation |
|
| Substring |
|
|
|
Common patterns on Strings
- Two pointers: , where is the work done at each iteration, includes sliding window
- Building a string from joining an array is
Linked Lists
Common patterns on Linked Lists
- Reverse between position
i and j:
- Detect a cycle: using fast-slow pointers or hash map
Misc
- Sorting: , where is the size of the data being sorted
- DFS and BFS on a graph: , where is the number of nodes, is the number of edges, if each node is handled in other than iterating over edges
- DFS and BFS space complexity: typically , but if it's in a graph, might be to store the graph
- Dynamic programming time complexity: , where is the number of states and is the work done at each state
- Dynamic programming space complexity: , where is the number of states