Big O notation
lastmod 2021-05-22created 2015-11-0611 edits
Asymptotic notation is a method in number theory and analysis that expresses the growth behavior of a function by comparing it with other functions. It is used to simplify the complexity of algorithms and to simplify the tail of infinite series.
The following five notations are the most common.
- Big O notation
- Little o notation
- Big Omega (Ω) notation
- Little omega (ω) notation
- Big Theta (Θ) notation
Of these, by far the most widely used is big O notation, also called Landau notation(isn't it "big-O notation"?).
Notation
Big O notation (Big-O Notation) is mainly used for analysis based on an algorithm's running time.
- O(1): An algorithm with constant running time regardless of the number of input items.
- O(log N): When the input size is N, the running time is log2N steps.
- O(N): When the input size is N, the running time is N steps.
- O(N log N): When the input size is N, the running time is N*(log2N) steps.
- O(N2): When the input size is N, the running time is N2 steps.
- O(N3): When the input size is N, the running time is N3 steps.
- O(2n): When the input size is N, the running time is 2N steps.
- O(n!): When the input size is N, the running time is n(n-1)(n-2)... * 1(n!) steps.
ex) The basic solution to the traveling salesman problem (TSP).
Sorted by running time, they are as follows. (Slower toward the right)
BigO-graph.png
Big-O Cheat Sheet
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) |
| Θ(n) | Θ(n) | Θ(1) | Θ(1) | O(n) | O(n) | O(1) | O(1) | O(n) | |
| Θ(n) | Θ(n) | Θ(1) | Θ(1) | O(n) | O(n) | O(1) | O(1) | 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) |
Array Sorting Algorithms
| Algorithm | Time Complexity | Space Complexity | ||
| Best | Average | Worst | Worst | |
| Ω(n log(n)) | Θ(n log(n)) | O(n^2) | O(log(n)) | |
| Mergesort | Ω(n log(n)) | Θ(n log(n)) | O(n log(n)) | O(n) |
| Timsort | Ω(n) | Θ(n log(n)) | O(n log(n)) | O(n) |
| Ω(n log(n)) | Θ(n log(n)) | O(n log(n)) | O(1) | |
| Bubble Sort | Ω(n) | Θ(n^2) | O(n^2) | O(1) |
| Insertion Sort | Ω(n) | Θ(n^2) | O(n^2) | O(1) |
| Selection Sort | Ω(n^2) | Θ(n^2) | O(n^2) | O(1) |
| Tree Sort | Ω(n log(n)) | Θ(n log(n)) | O(n^2) | O(n) |
| Shell Sort | Ω(n log(n)) | Θ(n(log(n))^2) | O(n(log(n))^2) | O(1) |
| Bucket Sort | Ω(n+k) | Θ(n+k) | O(n^2) | O(n) |
| Radix Sort | Ω(nk) | Θ(nk) | O(nk) | O(n+k) |
| Counting Sort | Ω(n+k) | Θ(n+k) | O(n+k) | O(k) |
| Cubesort | Ω(n) | Θ(n log(n)) | O(n log(n)) | O(n) |
Big-O Cheat Sheet Poster
Big-o-cheat-sheet-poster.png
Time complexity
(Reachable via Time complexity)
Time complexity is the result of analyzing an algorithm's running time.
Space complexity
(Reachable via Space complexity)
Space complexity is the result of analyzing an algorithm's memory usage.
See also
Favorite site
- Wikipedia (en) explanation of big O notation
- Big O notation / Big O analysis (Big O Notation)
- Big-oh(O) big O notation, time complexity
- Catching up on Big O notation for beginners
- Big O notation