Notes

Time and Space Complexity [ English ]

< Prev Next >

1. What is Complexity Analysis?

Complexity Analysis is the process of evaluating how efficient an algorithm is in terms of:

It helps compare algorithms and choose the most efficient one.

2. Time Complexity

Definition

Time Complexity measures the amount of time an algorithm takes to execute as a function of the input size ( n ).

Why Time Complexity Matters

Common Time Complexities

Complexity Name Example
O(1) Constant Access array element
O(log n) Logarithmic Binary Search
O(n) Linear Loop through array
O(n log n) Linearithmic Merge Sort
O(n²) Quadratic Nested loops
O(2ⁿ) Exponential Recursion (Fibonacci)

Examples

Constant Time — O(1)

x = arr[0]

Linear Time — O(n)

for i in range(n):
    print(i)

Quadratic Time — O(n²)

for i in range(n):
    for j in range(n):
        print(i, j)

Types of Time Complexity

1. Best Case

2. Worst Case

3. Average Case

3. Asymptotic Notations

Used to describe growth rate:

1. Big-O (O) → Upper Bound

2. Big-Theta (Θ) → Exact Bound

3. Big-Omega (Ω) → Lower Bound

4. Space Complexity

Definition

Space Complexity measures the total memory required by an algorithm as a function of input size.

Components of Space Complexity

1. Fixed Part

2. Variable Part

Examples

Constant Space — O(1)

a = 10
b = 20

Linear Space — O(n)

arr = [0] * n

Recursive Space

def func(n):
    if n == 0:
        return
    func(n-1)

5. Time vs Space Trade-Off

Sometimes:

Example:

6. Key Observations

7. Growth Comparison (Important for Exams)

O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ)

8. Final Insight

Short Exam Answer

Time Complexity: The time required by an algorithm as a function of input size. Space Complexity: The memory required by an algorithm as a function of input size.

Summary

Understanding time and space complexity is essential for:

< Prev Next >