what is Big Theta complexity? Explain in the easiest way possible with examples
Big Theta (Θ) notation is a way of describing the complexity of an algorithm, particularly how the run time or space requirements grow as the size of the input increases. It provides a tight bound on the growth rate of the algorithm, meaning that it describes the exact asymptotic behavior of the algorithm in terms of its upper and lower limits.
In simpler terms, if you know that an algorithm's running time grows in a particular way, you can use Big Theta to express that growth rate clearly and precisely.
### Understanding Big Theta (Θ) Notation
- **Θ(g(n))** means that for sufficiently large input sizes (n), the running time of the algorithm will grow neither faster nor slower than a constant multiple of **g(n)**.
- It effectively means the same as saying, "The algorithm runs in time that is proportional to g(n) for large inputs."
### Formal Definition
An algorithm is said to be in Θ(g(n)) if there are positive constants C₁, C₂, and n₀ such that for all n ≥ n₀:
\[ C₁ * g(n) ≤ T(n) ≤ C₂ * g(n) \]
Here:
- **T(n)** is the running time of the algorithm for input size n.
- **g(n)** is a function that describes the time complexity.
- **C₁** and **C₂** are constants that help define the boundaries.
- **n₀** is a threshold beyond which the Θ relationship holds true.
### Examples
1. **Constant Time - Θ(1)**:
- Example: Accessing an element in an array by index.
- Regardless of the size of the array, it takes the same amount of time to access any index, so we say it is Θ(1).
2. **Linear Time - Θ(n)**:
- Example: Finding the maximum value in an array.
- You have to check each element, so the time grows linearly with the size of the array. If the array size doubles, the time approximately doubles, which is Θ(n).
3. **Quadratic Time - Θ(n²)**:
- Example: Bubble Sort or Selection Sort.
- To sort an array of size n, you typically need to go through the array multiple times, leading to running times that are proportional to the square of the size of the input. Thus, it is Θ(n²).
4. **Logarithmic Time - Θ(log n)**:
- Example: Binary Search.
- In a sorted array, with each step, you cut the search space in half. This means that the number of operations grows logarithmically as the input size increases, which is expressed as Θ(log n).
### Summary
Big Theta gives you a precise way to express how the running time of an algorithm changes as the input size increases. It helps you understand not just the worst-case or best-case scenarios but provides a balanced view of performance, guiding you in selecting algorithms based on their expected behavior for large inputs.