VOOZH about

URL: https://www.geeksforgeeks.org/dsa/how-to-analyse-loops-for-complexity-analysis-of-algorithms/

⇱ Analysis of Loops in Algorithms - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Analysis of Loops in Algorithms

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:

  • Determine the number of iterations of the loop.
  • Determine the number of operations performed in each iteration of the loop.
  • Express the total number of operations performed by the loop as a function of the input size.
  • Determine the order of growth of the expression for the number of operations performed by the loop.

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).

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.

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.


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. 


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. 


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:


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.

  • We evaluate the situation when values in if-else conditions cause a maximum number of statements to be executed. 
    For example, consider the linear search function where we consider the case when an element is present at the end or not present at all. 
  • When the code is too complex to consider all if-else cases, we can get an upper bound by ignoring if-else and other complex control statements. 
Comment
Article Tags: