Skip to content
Home ยป Time and space complexity of an algorithm

Time and space complexity of an algorithm


๐Ÿ“˜ Time and Space Complexity of an Algorithm

1๏ธโƒฃ Introduction

The efficiency of an algorithm is measured mainly using two parameters:

  1. Time Complexity โ€“ how much time an algorithm takes to execute
  2. 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:

  1. Input space
  2. 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

FeatureTime ComplexitySpace Complexity
FocusExecution speedMemory usage
Measured inOperationsMemory units
GoalFaster algorithmMemory-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.