๐ Time and Space Complexity of an Algorithm
1๏ธโฃ Introduction
The efficiency of an algorithm is measured mainly using two parameters:
- Time Complexity โ how much time an algorithm takes to execute
- Space Complexity โ how much memory an algorithm uses during execution
Both are analyzed with respect to input size (n) and are expressed using asymptotic notations such as O, ฮฉ, and ฮ.
2๏ธโฃ Time Complexity
๐น Definition
Time complexity is the function that describes the amount of time an algorithm takes to run as a function of the input size n.
It does not measure actual time in seconds, but rather counts the number of basic operations performed.
๐น Factors Affecting Time Complexity
- Size of input (n)
- Number of loops
- Number of nested loops
- Conditional statements
- Recursive calls
3๏ธโฃ Types of Time Complexity
๐น Best Case
- Minimum time required
- Most favorable input
๐ Example:
Linear Search โ element found at first position
Best Case = O(1)
๐น Average Case
- Expected time for random input
- Difficult to calculate
๐ Example:
Linear Search โ O(n)
๐น Worst Case
- Maximum time required
- Most unfavorable input
- Most commonly used in exams
๐ Example:
Linear Search โ element found at last position
Worst Case = O(n)
4๏ธโฃ Time Complexity Analysis with Examples
๐น Constant Time โ O(1)
a = b + c
Execution time is constant.
๐น Linear Time โ O(n)
for i = 1 to n
print i
๐น Quadratic Time โ O(nยฒ)
for i = 1 to n
for j = 1 to n
print i, j
๐น Logarithmic Time โ O(log n)
Binary Search
๐น Linear Logarithmic โ O(n log n)
Merge Sort
5๏ธโฃ Space Complexity
๐น Definition
Space complexity is the total amount of memory used by an algorithm during execution.
It includes:
- Input space
- Auxiliary space (extra space used by algorithm)
๐น Components of Space Complexity
1. Input Space
Memory required to store input data.
๐ Example:
- Array of size n โ O(n)
2. Auxiliary Space
Extra memory used by algorithm (variables, recursion stack, temp arrays).
๐ Example:
- Merge Sort uses extra array โ O(n)
6๏ธโฃ Space Complexity Examples
๐น Constant Space โ O(1)
int a, b, c;
๐น Linear Space โ O(n)
int arr[n];
๐น Recursive Space
factorial(n)
- Stack space = O(n)
7๏ธโฃ Comparison: Time vs Space Complexity
| Feature | Time Complexity | Space Complexity |
|---|---|---|
| Focus | Execution speed | Memory usage |
| Measured in | Operations | Memory units |
| Goal | Faster algorithm | Memory-efficient algorithm |
8๏ธโฃ Trade-off Between Time and Space
Sometimes:
- Faster algorithms use more memory
- Memory-efficient algorithms take more time
๐ Example:
- Using hashing increases space but reduces time
9๏ธโฃ Importance in Algorithm Design
- Helps choose optimal algorithm
- Improves program efficiency
- Critical for large data processing
- Foundation for advanced techniques (DP, Greedy, D&C)
๐ Exam-Oriented Key Points
- Worst-case time complexity is preferred
- Constants are ignored
- Space complexity includes recursion stack
- Always express complexity using Big-O notation
๐ Conclusion
Time and space complexity analysis allows us to evaluate algorithm performance and choose the most efficient solution.
An efficient algorithm balances both time and space.
