|
| 1 | +# Complexity Analysis |
| 2 | + |
| 3 | +Complexity analysis evaluates the efficiency of algorithms in terms of time and space. The goal is to understand how an algorithm performs as input size grows, using Big O, Big Θ, and Big Ω notations. |
| 4 | + |
| 5 | +## 1. Time Complexity |
| 6 | +Time complexity measures the time taken by an algorithm to run as a function of the input size \(n\). |
| 7 | + |
| 8 | +- **Big O (O)**: Upper bound. Worst-case scenario. |
| 9 | +- **Big Θ (Θ)**: Tight bound. Average-case scenario. |
| 10 | +- **Big Ω (Ω)**: Lower bound. Best-case scenario. |
| 11 | + |
| 12 | +### Common Time Complexities |
| 13 | +| Complexity | Description | Example | |
| 14 | +|-----------------|--------------------------------------|------------------------------| |
| 15 | +| **O(1)** | Constant time | Accessing an array element | |
| 16 | +| **O(log n)** | Logarithmic | Binary search | |
| 17 | +| **O(n)** | Linear | Linear search | |
| 18 | +| **O(n log n)** | Linearithmic | Merge sort, heapsort | |
| 19 | +| **O(n^2)** | Quadratic | Bubble sort, insertion sort | |
| 20 | +| **O(2^n)** | Exponential | Recursive Fibonacci | |
| 21 | +| **O(n!)** | Factorial | Permutation generation | |
| 22 | + |
| 23 | +## 2. Space Complexity |
| 24 | +Space complexity measures the amount of memory required by an algorithm as a function of input size. |
| 25 | + |
| 26 | +- Includes both auxiliary space and input space. |
| 27 | +- Important for memory-intensive applications. |
| 28 | + |
| 29 | +## 3. Asymptotic Notations |
| 30 | +These notations describe the growth rate of functions: |
| 31 | + |
| 32 | +- **Big O (O)**: Upper limit; worst-case behavior. |
| 33 | +- **Big Θ (Θ)**: Average or "tight" bound; commonly expected performance. |
| 34 | +- **Big Ω (Ω)**: Lower limit; best-case behavior. |
| 35 | + |
| 36 | +## 4. Trade-offs |
| 37 | +- **Time vs Space**: Faster algorithms often use more memory, and vice versa. |
| 38 | +- **Amortized Analysis**: For operations like dynamic array resizing, considers average time per operation over a sequence. |
| 39 | + |
| 40 | +Understanding complexity is key to choosing the most efficient algorithm for a given problem. |
0 commit comments