Home » Data Structure » Unit 1: Introduction to Data Structure » How to Determine the Complexity of an Algorithm with Example

How to Determine the Complexity of an Algorithm with Example

Last updated:

Support this post with a reaction:

5,301 views

Time and space complexity are two important measures used to analyze the efficiency of an algorithm. Time complexity determines how the execution time of an algorithm grows with input size, while space complexity determines how much memory an algorithm requires.

To find the complexity of an algorithm, identify the input size, count the basic operations, analyze loops and recursive calls, and express the growth rate using asymptotic notation such as Big O.

In this tutorial, you will learn how to calculate time and space complexity step by step with examples in programming.

Algorithm Complexity

Algorithm complexity is a measure of the resources required by an algorithm to solve a problem as the input size increases.

There are two major types of algorithm complexity:

  1. Time Complexity: Measures how the number of operations grows with input size.
  2. Space Complexity: Measures how the memory requirements grow with input size.

For example, consider an algorithm that searches for an element in an array containing n elements.

  • If it checks only the first element, it performs one comparison.
  • If it checks every element, it may perform n comparisons.

Therefore, the number of operations depends on the input size.

Types of Asymptotic Notations

Asymptotic notations describe how the performance of an algorithm grows as the input size approaches a large value.

The three commonly used notations are:

1. Big O Notation – O()

Big O notation represents an asymptotic upper bound on an algorithm’s growth rate. It is commonly used to describe worst-case time complexity.

Example: Linear Search has a worst-case time complexity of O(n).

2. Omega Notation – Ω()

Omega notation represents an asymptotic lower bound on an algorithm’s growth rate.

Example: Linear Search has a best-case time complexity of Ω(1), when the target element is found at the first position.

3. Theta Notation – Θ()

Theta notation represents a tight asymptotic bound when the upper and lower bounds have the same growth rate.

Example: A loop that always executes exactly n times has a time complexity of Θ(n).

 Time Complexity Comparision

How to Find the Time Complexity of an Algorithm Step by Step?

Follow these steps to calculate the time complexity of any algorithm.

  1. Identify the input size (n).
  2. Identify the basic operations.
  3. Count how many times each operation executes.
  4. Analyze loops and conditional statements.
  5. Analyze recursive function calls, if present.
  6. Ignore constants and lower-order terms.
  7. Express the final result using Big O notation.

Example 1: How to Find the Complexity of a Simple Statement

Consider the following C program:

#include <stdio.h>

int main() {
    int a = 10;
    int b = 20;
    int sum = a + b;

    printf("%d", sum);

    return 0;
}

Analysis:

The statements execute a fixed number of times, regardless of the input size.

Therefore:

  • Assignment operations: Constant
  • Addition operation: Constant
  • Print operation: Constant

Time Complexity: O(1)

This is called constant time complexity.

Example 2: How to Find the Complexity of a Single Loop?

Consider the following C program:

#include <stdio.h>

int main() {
    int n = 5;

    for (int i = 0; i < n; i++) {
        printf("%d ", i);
    }

    return 0;
}

Analysis:

The loop executes n times.

OperationNumber of executions
Initialization1
Condition checkingn + 1
Incrementn
Print statementn

The total number of operations is proportional to n.

Therefore:

Time Complexity: O(n)

Example 3: How to Find the Complexity of Nested Loops?

Consider the following C program:

#include <stdio.h>

int main() {
    int n = 5;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d %d\n", i, j);
        }
    }

    return 0;
}

Analysis:

  • The outer loop executes n times.
  • For every outer loop iteration, the inner loop executes n times.
  • Therefore, the inner statement executes n × n times.

Total operations:

n×n=n2 n \times n = n^2

Time Complexity: O(n²)

Example 4: How to Find the Complexity of Consecutive Loops?

Consider the following code:

for (int i = 0; i < n; i++) {
    printf("%d ", i);
}

for (int j = 0; j < n; j++) {
    printf("%d ", j);
}

Analysis:

  • First loop executes n times.
  • Second loop executes n times.
  • Both loops execute independently.

Total operations:

T(n)=n+n=2n T(n) = n + n = 2n

After removing the constant factor:

Time Complexity: O(n)

Example 5: How to Find the Complexity When the Loop Variable Is Doubled?

Consider the following C program:

#include <stdio.h>

int main() {
    int n = 16;

    for (int i = 1; i < n; i = i * 2) {
        printf("%d ", i);
    }

    return 0;
}

Analysis:

The value of i doubles during every iteration.

The values are:

1, 2, 4, 8

The number of iterations is approximately log₂(n).

Therefore:

Time Complexity: O(log n)

Example 6: How to Find the Complexity of a Recursive Algorithm?

Consider the following recursive function:

int factorial(int n) {
    if (n == 0) {
        return 1;
    }

    return n * factorial(n - 1);
}

Analysis:

  • Each function call decreases n by 1.
  • The function continues until n becomes 0.
  • Therefore, approximately n recursive calls are performed.

Recurrence relation:

T(n)=T(n−1)+O(1) T(n) = T(n-1) + O(1)

Expanding the recurrence gives:

T(n)=O(n) T(n) = O(n)

Time Complexity: O(n)

How to Find the Space Complexity of an Algorithm?

Space complexity measures the memory required by an algorithm as the input size increases.

It includes the memory required for input data and additional memory used during execution.

There are two important categories:

1. Input Space: Memory required to store the input data.

2. Auxiliary Space: Extra memory used by the algorithm, excluding the input storage itself.

Let’s understand space complexity with examples.

Example 1: Constant Space Complexity

int sum(int a, int b) {
    int result = a + b;
    return result;
}
Analysis:

The function uses a fixed number of variables, regardless of the input values.

Therefore:

Auxiliary Space Complexity: O(1)

Example 2: Linear Space Complexity


Analysis:
#include <stdlib.h>

int main() {
    int n = 10;
    int *arr = malloc(n * sizeof(int));

    free(arr);
    return 0;
}
  • The program dynamically allocates memory for n integers.
  • The memory requirement increases linearly with n.

Therefore:

Auxiliary Space Complexity: O(n)

Example 3: Space Complexity of a Recursive Function

int factorial(int n) {
    if (n == 0) {
        return 1;
    }

    return n * factorial(n - 1);
}
Analysis:
  • Each recursive call creates a new stack frame.
  • The maximum recursion depth is proportional to n.
  • Each stack frame requires constant additional memory.

Therefore:

Auxiliary Space Complexity: O(n)