Tutorials

Basics Time Complexity Analysis

Papri Ghosh
21 May 2018
Read Time : 15 Minutes
Basics Time Complexity Analysis

The time complexity analysis is the main concern on Design and Analysis of Algorithms. Before showing the notation that helps to analysis the time complexity of an algorithm, we must know how the time complexity of a simple code can be calculated.

For each the following reasons let us consider one unit of time:

1.    Define a variable
2.    Assigning a value to a variable
3.    One operation i.e. addition, subtraction etc.
4.    Printing
5.    Scanning Values
6.    Function Call etc.

Few small programming codes along with their required time is given:

Example 1:

int i, j; 2 7
i=4; 1  
j=12; 1  
i=j/i;  1+1  
printf(“%d”,i); 1  

Total Required Time = 7

Example 2:

int a, b;   2 5
a=12;  1  
b=2; 1  
if(a 1  
{ 0 1
     printf(“%d”, i); 1  
} 0  
else 0 1
{ 0  
     printf(“%d”, j); 1  
}    

Total Required Time = 6

Example 3:

int a, b;   2 5
a=12;  1  
b=2; 1  
if(a 1  
{ 0 2
     printf(“%d”, i); 1  
     printf(“Hello”); 1  
} 0  
else 0 1
{ 0  
     printf(“%d”, j); 1  
} 0  

Total Required Time = 7

Example 4:

int i, n; 2  
n=3; 1  
for(i=1; i<=n; i=i+2) 1+(n+1)+n  
{ 0 2 x n
    printf(“%d”, i); 1  
    printf(“Hello”);  1  
} 0  

Total Required Time = 5 + 4n (May varies based on the programming code)

In this way it will be very easy to calculate the time complexity of a given programming code. Based on this approach, the total time complexity is calculated for different problems.