Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Step Count Method for Time Complexity Analysis

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

What is Time Complexity?

Time Complexity is the amount of time taken by the algorithm to run. It measures the time taken to execute each statement of code in an algorithm.

Time Complexity can be calculated by using Two types of methods. They are:

  • Step Count Method
  • Asymptotic Notation.

Here, we will discuss the Step Count Method.

What is Step Count Method?

The step count method is one of the methods to analyze the Time complexity of an algorithm. In this method, we count the number of times each instruction is executed. Based on that we will calculate the Time Complexity. 

The step Count method is also called as Frequency Count method. Let us discuss step count for different statements:


  • Comments are used for giving extra meaning to the program. They are not executed during the execution. Comments are ignored during execution.
  • Therefore the number of times that a comment executes is 0.

2. Conditional statements:

Conditional statements check the condition and if the condition is correct then the conditional subpart will be executed. So the execution of conditional statements happens only once. The compiler will execute the conditional statements to check whether the condition is correct or not so it will be executed one time.

  • In if-else statements the if statement is executed one time but the else statement will be executed zero or one time because if the “if” statement is executed then the else statement will not execute.
  • In switch case statements the starting switch(condition) statement will be executed one time but the inner case statements will execute if none of the previous case statements are executed.
  • In nested if and if else ladder statements also the initial if statement is executed at least once but inner statements will be executed based on the previous statements’ execution.

3. Loop statements:

Loop statements are iterative statements. They are executed one or more times based on a given condition.

  • A typical for(i = 0; i ≤ n; i++) statement will be executed “n+1” times for the first n times the condition is satisfied and the inner loop will be executed and for the (n+1)th time the condition is failed and the loop terminates.
  • While: The statement is executed until the given condition is satisfied.
  • Do while: The statement will repeat until the given condition is satisfied. The do-while statement will execute at least once because for the first time it will not check the condition.

4. Functions:

Functions are executed based on the number of times they get called. If they get called n times they will be executed n times. If they are not called at least once then they will not be executed. Other statements like BEGIN, END and goto statements will be executed one time.

Illustration of Step Count Method:

Analysis of Linear Search algorithm

Let us consider a Linear Search Algorithm.

Linearsearch(arr, n, key)                                 
    i = 0;
    for(i = 0; i < n; i++)
        if(arr[i] == key)


  • i = 0, is an initialization statement and takes O(1) times.
  • for(i = 0;i < n ; i++), is a loop and it takes O(n+1) times .
  • if(arr[i] == key), is a conditional statement and takes O(1) times.
  • printf(“Found”), is a function and that takes O(0)/O(1) times.

Therefore Total Number of times it is executed is n + 4 times. As we ignore lower exponents in time complexity total time became O(n).

Time complexity: O(n).
Auxiliary Space: O(1)

Linear Search in Matrix

Searching for an element in a matrix

Algo Matrixsearch(mat[][], key)

    // number of rows;
    r := len(mat)

    // number of columns;
    c := len(mat[0])     

    for(i = 0; i < r; i++)
        for(j = 0;  j < c;  j++)
            if(mat[i][j] == key)
                printf(“Element found”);


  •  r = len(mat), takes O(1) times.
  •  c = len(mat[0]), takes O(1) times
  •  for(i = 0; i < r; i++), takes O(r + 1) times
  •  for(j = 0; j < c; j++), takes O(( c + 1 ) ) for each time the outer loop is satisfied. So total r times the loop is executed.
  •  if(mat[i][j] == key), takes O(1) times 
  • printf(“Element found”), takes O(0)/O(1) times.

Therefore Total Number of times it is executed is (1 + 1 + (r + 1) + (r) * (c + 1) + 1) times. As we ignore the lower exponents, total complexity became O(r * (c + 1)).

the mat is an array so it takes n*n words , k, c, r, i, j will take 1 word.

Time Complexity: O(n2).

Auxiliary Space: O(n2)

In this way, we calculate the time complexity by counting the number of times each line executes.

Advantages of using this method over others:

  • Easy to understand and implement.
  • We will get an exact number of times each statement is executed.

Related Articles:

My Personal Notes arrow_drop_up
Last Updated : 04 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials