Analysis of Loops in Algorithms (original) (raw)

Last Updated : 29 Jan, 2026

The analysis of loops for the complexity analysis of algorithms involves finding the number of operations performed by a loop as a function of the input size. The following are the general steps to analyze loops for complexity analysis:

Constant Time Complexity O(1):

O(1) refers to constant time means that the running time of an algorithm remains constant and does not depend on the size of the input. A loop or recursion that runs a constant number of times is also considered O(1). For example, the following loop is O(1).

C++ `

for (int i = 1; i <= 10; i++) { // some O(1) expressions }

C

for (int i = 1; i <= 10; i++) { // some O(1) expressions }

Java

for (int i = 1; i <= 10; i++) { // some O(1) expressions }

Python

for i in range(1, 11): # some O(1) expressions

C#

// Here c is a positive constant for (int i = 1; i <= 10; i++) { // Some O(1) work }

JavaScript

for (let i = 1; i <= 10; i++) { // some O(1) expressions }

`

**Example : Swap function

Linear Time Complexity O(n):

The Time Complexity is O(n) if the loop variables are incremented/decremented by a constant amount. For example, searching for an element in an unsorted array or iterating through an array and performing a constant amount of work for each element.

C++ `

// Here c is a positive integer constant for (int i = 1; i <= n; i = i + c) { // some O(1) expressions }

for (int i = n; i > 0; i = i - c) { // some O(1) expressions }

C

// Here c is a positive integer constant for (int i = 1; i <= n; i += c) { // some O(1) expressions }

for (int i = n; i > 0; i -= c) { // some O(1) expressions }

Java

// Here c is a positive integer constant for (int i = 1; i <= n; i += c) { // some O(1) expressions }

for (int i = n; i > 0; i -= c) { // some O(1) expressions }

Python

Here c is a positive integer constant

for i in range(1, n+1, c): # some O(1) expressions

for i in range(n, 0, -c): # some O(1) expressions

C#

for (int i = 1; i <= n; i = i + c) { // some O(1) expressions

// O(1) expressions could be computations, assignments,
// or other constant time operations

}

// Second loop: Decrementing by 'c' from n to 1 for (int i = n; i > 0; i = i - c) { // some O(1) expressions

// O(1) expressions could be computations, assignments,
// or other constant time operations

}

JavaScript

// Here c is a positive integer constant for (var i = 1; i <= n; i += c) { // some O(1) expressions }

for (var i = n; i > 0; i -= c) { // some O(1) expressions }

`

Quadratic Time Complexity O(nc):

The time complexity is defined as an algorithm whose performance is directly proportional to the squared size of the input data, as in nested loops it is equal to the number of times the innermost statement is executed.

C++ `

// Here c is any positive constant for (int i = 1; i <= n; i += c) { for (int j = 1; j <= n; j += c) { // some O(1) expressions } }

for (int i = n; i > 0; i -= c) { for (int j = i + 1; j <= n; j += c) { // some O(1) expressions } }

for (int i = n; i > 0; i -= c) { for (int j = i - 1; j > 0; j -= c) { // some O(1) expressions } }

C

for (int i = 1; i <= n; i += c) { for (int j = 1; j <= n; j += c) { // some O(1) expressions } }

for (int i = n; i > 0; i -= c) { for (int j = i + 1; j <= n; j += c) { // some O(1) expressions } }

Java

for (int i = 1; i <= n; i += c) { for (int j = 1; j <= n; j += c) { // some O(1) expressions } }

for (int i = n; i > 0; i -= c) { for (int j = i + 1; j <= n; j += c) { // some O(1) expressions } }

Python

for i in range(1, n+1, c): for j in range(1, n+1, c): # some O(1) expressions

for i in range(n, 0, -c): for j in range(i+1, n+1, c): # some O(1) expressions

C#

using System;

class Program { static void Main() { // Here c is any positive constant int n = 10; // You can replace 10 with your desired // value of 'n' int c = 2; // You can replace 2 with your desired // value of 'c'

    // First loop
    for (int i = 1; i <= n; i += c) {
        for (int j = 1; j <= n; j += c) {
            // some O(1) expressions
            Console.WriteLine("Expression at (" + i
                              + ", " + j + ")");
        }
    }

    // Second loop
    for (int i = n; i > 0; i -= c) {
        for (int j = i + 1; j <= n; j += c) {
            // some O(1) expressions
            Console.WriteLine("Expression at (" + i
                              + ", " + j + ")");
        }
    }

    // Third loop
    for (int i = n; i > 0; i -= c) {
        for (int j = i - 1; j > 0; j -= c) {
            // some O(1) expressions
            Console.WriteLine("Expression at (" + i
                              + ", " + j + ")");
        }
    }
}

}

JavaScript

for (var i = 1; i <= n; i += c) { for (var j = 1; j <= n; j += c) { // some O(1) expressions } }

for (var i = n; i > 0; i -= c) { for (var j = i + 1; j <= n; j += c) { // some O(1) expressions } }

`

**Example: Selection sort and Insertion Sort have O(n2) time complexity.

Logarithmic Time Complexity O(Log n):

The time Complexity of a loop is considered as O(Logn) if the loop variables are divided/multiplied by a constant amount.

C++ `

for (int i = 1; i <= n; i *= c) { // some O(1) expressions } for (int i = n; i > 0; i /= c) { // some O(1) expressions }

C

for (int i = 1; i <= n; i *= c) { // some O(1) expressions } for (int i = n; i > 0; i /= c) { // some O(1) expressions }

Java

for (int i = 1; i <= n; i *= c) { // some O(1) expressions } for (int i = n; i > 0; i /= c) { // some O(1) expressions }

Python

i = 1 while(i <= n): # some O(1) expressions i = i*c

i = n while(i > 0): # some O(1) expressions i = i//c

C#

using System;

class Program { static void Main(string[] args) { int n = 10; // assuming n is some integer value int c = 2; // assuming c is some integer value

    // Loop to iterate through powers of c up to n
    for (int i = 1; i <= n; i *= c) {
        // O(1) expressions here
        Console.WriteLine("i = " + i);
    }

    // Loop to iterate through powers of c down from n
    for (int i = n; i > 0; i /= c) {
        // O(1) expressions here
        Console.WriteLine("i = " + i);
    }
}

}

JavaScript

for (var i = 1; i <= n; i *= c) { // some O(1) expressions } for (var i = n; i > 0; i /= c) { // some O(1) expressions }

C++

// Recursive function void recurse(int n) { if (n <= 0) return; else { // some O(1) expressions } recurse(n/c); // Here c is positive integer constant greater than 1 } // This code is contributed by Kshitij

C

// Recursive function void recurse(int n) { if (n <= 0) return; else { // some O(1) expressions } recurse(n/c); // Here c is positive integer constant greater than 1 }

Java

// Recursive function void recurse(int n) { if (n <= 0) return; else { // some O(1) expressions } recurse(n/c); // Here c is positive integer constant greater than 1

} // This code is contributed by Utkarsh

Python

Recursive function

def recurse(n): if(n <= 0): return else: # some O(1) expressions recurse(n/c)

Here c is positive integer constant greater than 1

This code is contributed by Pushpesh Raj

C#

using System;

class Program { // Recursive function static void Recurse(int n, int c) { // Base case: If n is less than or equal to 0, // return if (n <= 0) return; else { // Perform some O(1) expressions

        // Recursive call with updated parameter (n/c)
        Recurse(n / c, c);
    }
}

static void Main()
{
    int n = 10; // Example value for n
    int c = 2; // Example value for c

    // Function Call
    Recurse(n, c);

    Console.WriteLine("Recursive function executed.");
}

}

JavaScript

// Recursive function function recurse(n) { if (n <= 0) return; else { // some O(1) expressions } recurse(n/c); // Here c is positive integer constant greater than 1 }

`

**Example: Binary Search(refer iterative implementation) has O(Logn) time complexity.

Log Log Time Complexity O(Log Log n):

The Time Complexity of a loop is considered as O(Log Log n) if the loop variables are reduced/increased exponentially by a constant amount.

C++ `

// Here c is a constant greater than 1 for (int i = 2; i <= n; i = pow(i, c)) { // some O(1) expressions }

// Here fun() is sqrt or cuberoot or any // other constant root for (int i = n; i > 1; i = fun(i)) { // some O(1) expressions }

C

// Here c is a constant greater than1 for (int i = 2; i <= n; i = pow(i, c)) { // some O(1) expressions }

// Here fun is sqrt or cuberoot or any // other constant root for (int i = n; i > 1; i = fun(i)) { // some O(1) expressions }

Java

// Here c is a constant greater than 1 for (int i = 2; i <= n; i = Math.pow(i, c)) { // some O(1) expressions }

// Here fun is sqrt or cuberoot or any // other constant root for (int i = n; i > 1; i = fun(i)) { // some O(1) expressions }

Python

Here c is a constant greater than 1

i = 2 while(i <= n): # some O(1) expressions i = i**c

Here fun is sqrt or cuberoot or any other constant root

i = n while(i > 1): # some O(1) expressions i = fun(i)

C#

using System;

public class Main { public static void Execute(string[] args) { int n = 100; // Example value of n int c = 2; // Example value of c // Here c is a constant greater than 1 for (int i = 2; i <= n; i = (int)Math.Pow(i, c)) { // some O(1) expressions Console.WriteLine(i); // For demonstration }

    // Here fun() is sqrt or cuberoot or any other constant root
    for (int i = n; i > 1; i = fun(i))
    {
        // some O(1) expressions
        Console.WriteLine(i); // For demonstration
    }
}

// Function to find constant root (e.g., sqrt, cuberoot)
public static int fun(int num)
{
    // Here, let's consider finding the square root
    return (int)Math.Sqrt(num);
}

}

JavaScript

// Here c is a constant greater than 1 for (var i = 2; i <= n; i = i**c) { // some O(1) expressions }

// Here fun is sqrt or cuberoot or any // other constant root for (var i = n; i > 1; i = fun(i)) { // some O(1) expressions }

`

See this for mathematical details.

**How to combine the time complexities of consecutive loops?

When there are consecutive loops, we calculate time complexity as a sum of the time complexities of individual loops. For example, consider the following code:

C++ `

//Here c is any positive constant for (int i = 1; i <= m; i += c) { // some O(1) expressions } for (int i = 1; i <= n; i += c) { // some O(1) expressions }

// Time complexity of above code is O(m) + O(n) which is O(m + n) // If m == n, the time complexity becomes O(2n) which is O(n).

C

for (int i = 1; i <= m; i += c) { // some O(1) expressions } for (int i = 1; i <= n; i += c) { // some O(1) expressions }

// Time complexity of above code is O(m) + O(n) which is O(m + n) // If m == n, the time complexity becomes O(2n) which is O(n).

Java

for (int i = 1; i <= m; i += c) { // some O(1) expressions } for (int i = 1; i <= n; i += c) { // some O(1) expressions }

// Time complexity of above code is O(m) + O(n) which is O(m + n) // If m == n, the time complexity becomes O(2n) which is O(n).

Python

for i in range(1, m+1, c): # some O(1) expressions

for i in range(1, n+1, c): # some O(1) expressions

Time complexity of above code is O(m) + O(n) which is O(m + n)

If m == n, the time complexity becomes O(2n) which is O(n).

C#

// Here c is any positive constant for (int i = 1; i <= m; i += c) { // some O(1) expressions } for (int i = 1; i <= n; i += c) { // some O(1) expressions }

// Time complexity of above code is O(m) + O(n) which is O(m + n) // If m == n, the time complexity becomes O(2n) which is O(n).

JavaScript

for (var i = 1; i <= m; i += c) { // some O(1) expressions } for (var i = 1; i <= n; i += c) { // some O(1) expressions }

// Time complexity of above code is O(m) + O(n) which is O(m + n) // If m == n, the time complexity becomes O(2n) which is O(n).

`

**How to calculate when there are many if, else statements inside loops?

As discussed here, the worst-case time complexity is the most useful among best, average and worst. Therefore we need to consider the worst case.