# Time & Space Complexity

### Time Complexity

Time complexity refers to the amount of time taken by an algorithm to complete its execution as a function of its input size. It is commonly expressed in terms of the "big O" notation, which provides an upper bound on the worst-case running time of an algorithm. The following table provides an overview of common time complexities and their corresponding "big O" notations:

<table><thead><tr><th width="207">Time Complexity</th><th width="164.33333333333331">Notation</th><th>Example</th></tr></thead><tbody><tr><td>Constant</td><td>O(1)</td><td>Accessing a specific element in an array</td></tr><tr><td>Logarithmic</td><td>O(log n)</td><td>Binary search</td></tr><tr><td>Linear</td><td>O(n)</td><td>Finding the maximum element in an array</td></tr><tr><td>Linearithmic</td><td>O(n log n)</td><td>Merge sort</td></tr><tr><td>Quadratic</td><td>O(n^2)</td><td>Bubble sort</td></tr><tr><td>Exponential</td><td>O(2^n)</td><td>Recursive Fibonacci</td></tr></tbody></table>

### Space Complexity

Space complexity refers to the amount of memory required by an algorithm to complete its execution as a function of its input size. It is commonly expressed in terms of the "big O" notation, which provides an upper bound on the worst-case space usage of an algorithm. The following table provides an overview of common space complexities and their corresponding "big O" notations:

<table><thead><tr><th width="196.33333333333331">Space Complexity</th><th width="186">Notation</th><th>Example</th></tr></thead><tbody><tr><td>Constant</td><td>O(1)</td><td>Allocating a fixed number of variables</td></tr><tr><td>Linear</td><td>O(n)</td><td>Allocating an array of size n</td></tr><tr><td>Quadratic</td><td>O(n^2)</td><td>Allocating a 2D array of size n x n</td></tr></tbody></table>

### Conclusion

Understanding time and space complexity is important for designing efficient algorithms. By analyzing the time and space requirements of an algorithm, software developers can choose the most appropriate algorithm for a given problem and optimize it for better performance. The "big O" notation provides a standardized way of expressing time and space complexity and helps in comparing algorithms with different input sizes.
