Time and Space Complexity - Big O Notation Guide
What is Time and Space Complexity?
- Time Complexity: How the runtime of an algorithm grows with input size n.
- Space Complexity: How the memory usage of an algorithm grows with input size n.
Big O Notation Basics
- O(1): Constant Time -> Accessing array by index
- O(log n): Logarithmic Time -> Binary Search
- O(n): Linear Time -> Loop through array
- O(n log n): Linearithmic -> Merge Sort, Quick Sort (avg)
- O(n**2): Quadratic -> Nested loops (Bubble Sort)
- O(2**n): Exponential -> Recursive Fibonacci
- O(n!): Factorial -> Brute-force permutations
Time Complexity Examples
- O(1): Access array element
- O(n): Sum array elements
- O(n**2): Print all pairs in array (nested loop)
- O(log n): Binary Search
Space Complexity Examples
- O(1): Finding max
- O(n): Copying array
Loop Complexity
- for i in range(n): O(n)
- for i in range(n): for j in range(n): O(n**2)
- for i in range(1, n, i*=2): O(log n)
Drop Constants and Lower Terms
- O(2n) -> O(n), O(n + log n) -> O(n), O(n**2 + n) -> O(n**2)
Case Analysis
- Best Case: Fastest (e.g. Binary Search O(1))
- Worst Case: Slowest (e.g. Binary Search O(log n))
- Average Case: Expected runtime
Recursion
Example: Fibonacci -> Time: O(2**n), Space: O(n)
Algorithm Cheat Sheet
- Linear Search: O(n)
- Binary Search: O(log n)
- Bubble Sort: O(n**2)
- Merge Sort: O(n log n)
- Quick Sort (avg): O(n log n)
- Insertion Sort: O(n**2)
- Hashing Lookup: O(1) avg, O(n) worst
Tips
- Dry run your code.
- Count operations in loops.
- Practice on GFG, LeetCode.
- Learn DS internal complexities.