Open In App
Related Articles

Guidelines for asymptotic analysis

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, the focus is on learning some rules that can help to determine the running time of an algorithm.

Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation. In Asymptotic Analysis, the performance of an algorithm in terms of input size (we don’t measure the actual running time) is evaluated. How the time (or space) taken by an algorithm increases with the input size is also calculated.

(g(n)) = {f(n)   such that g(n) is a curve which approximates f(n) at higher values of input size, n}

This curve is called asymptotic curve and the algorithm analysis for such a curve is called Asymptotic analysis.

Loops: The running time of a loop is, at most, the running time of the statements inside the loop, including tests) multiplied number of iterations.

Below is the python program that demonstrates the above concept:

Python3

# Python program to implement
# the above concept
# execute n times for in
# range(0.00);
 
for i in range(0, n):
print ('Current Number:', i, sep = "")

                    
#constant time 
Total time a constant cx n = cn = O(n).

Nested loops: Analyze from the inside out. The total running time is the product of the sizes of all the loops.

Below is a python program that demonstrates the above concept:

C++

// C++ program to implement
// the above concept
// outer loop executed n times
#include <iostream>
using namespace std;
 
int main()
{
    for(int i = 0; i < n; i++)
    {
 
    // inner loop executes n times
    for(int j = 0; j < n; j++)
        cout<<"i value " << i << " and j value " << j;
    }
}
 
// This code is contributed by Utkarsh

                    

Java

// Java program to implement
// the above concept
// outer loop executed n times
import java.io.*;
 
class GFG {
  public static void main (String[] args)
  {
    for(int i = 0; i < n; i++)
    {
 
      // inner loop executes n times
      for(int j = 0; j < n; j++)
        System.out.print("i value " + i + "  and j value " + j);
    }
  }
}
 
// This code is contributed by Aman Kumar

                    

Python3

# Python program to implement
# the above concept
# outer loop executed n times
for i in range(0, n):
 
    # inner loop executes n times
    for j in range(0, n):
      print("i value % d  and j value % d" % (i, j))

                    

C#

// C# program to implement
// the above concept
// outer loop executed n times
using System;
 
class GFG {
  public static void Main ()
  {
    for(int i = 0; i < n; i++)
    {
 
      // inner loop executes n times
      for(int j = 0; j < n; j++)
        Console.Write("i value " + i + " and j value " + j);
    }
  }
}
 
// This code is contributed by Pushpesh Raj

                    

Javascript

// JavaScript program to implement
// the above concept
// outer loop executed n times
let n = 10;
for(let i = 0; i < n; i++)
{
    // inner loop executes n times
    for(let j = 0; j < n; j++)
        console.log("i value " + i + " and j value " + j);
}

                    
# constant time 
Total time = C x n x n = cn^2 =0(n²).

Consecutive statements: Add the time complexity of each statement.

Below is a python program that demonstrates the above concept:

Python3

# Python program that implements
# the above concept
n = 100
 
# executes n times
for i in range (0, n):
    print (Current Number: i, sep = "")
     
    # outer loop executed n times
    for i in range (0, n):
       
      # inner loop executes n times
      for j in range(0, n):
        print(" i value % d and j value % d"%(i, j))
 
X

                    
Total time = co + c1n + c2n^2 = 0(n^2).

If-then-else statements: Worst-case running time: the test, plus either the then part of the else part whichever is the largest. 

Below is a python program that demonstrates the above concept:

Python3

# Python program that implements
# the above concept
if n == I:
  print ("Incorrect Value")
  print (n)
 
else:
  for i in range(0, n):
     
    # constant time
    print (CurrNumber:, i, sep = "")

                    
Total time = co + c1*n = 0(n).


Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads