Code Bits
Big O Notation
Visualize algorithm complexity with interactive D3.js charts. Understand how different time complexities scale.
Growth Comparison
Input size (n):20
Operations at n = 20
O(1)
1
O(log n)
4
O(n)
20
O(n log n)
86
O(n²)
400
If each operation takes 1μs:
O(1)1μs
O(log n)4μs
O(n)20μs
O(n log n)86μs
O(n²)400μs
Quick Reference
O(1)Constant
Executes in the same time regardless of input size. Array access, hash table lookup.
return arr[0];O(log n)Logarithmic
Halves the problem space each step. Binary search, balanced tree operations.
while (n > 1) n = n / 2;O(n)Linear
Time grows proportionally with input. Single loop through data.
for (i = 0; i < n; i++)O(n log n)Linearithmic
Efficient sorting algorithms. Merge sort, quicksort average case.
mergeSort(arr)O(n²)Quadratic
Nested loops over data. Bubble sort, comparing all pairs.
for (i) for (j)O(n³)Cubic
Triple nested loops. Naive matrix multiplication.
for (i) for (j) for (k)O(2ⁿ)Exponential
Doubles with each input increase. Recursive Fibonacci, power set.
f(n-1) + f(n-2)Code Examples
CodeO(1)
function getFirst(arr) {
return arr[0]; // Direct access
}Explanation
Accessing an array element by index takes constant time regardless of array size.
Common Algorithm Complexities
| Algorithm | Best | Average | Worst | Space |
|---|---|---|---|---|
| Binary Search | O(1) | O(log n) | O(log n) | O(1) |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) |
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) |
| Hash Table Lookup | O(1) | O(1) | O(n) | O(n) |
| BFS/DFS | O(V+E) | O(V+E) | O(V+E) | O(V) |