SANJIVANI K. B. P. POLYTECHNIC,
KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of
Technical Education, Mumbai, ISO 9001:2015 Certified Institute
Name of Faculty: Prof. Vaibhav A. Parjane
1
Topic to be Covered
Time Complexity
2
Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
Time Complexity :
1. Time complexity is the total time taken by the algorithm
to perform the intended task.
2. It is also known as frequency count.
3. Eg: consider the FOR LOOP
For ( i=0 ; i<n ; i++ )
Atleast once (1) + (n+1) + n
= 1 + (n+1) +n
= 2n + 2
true
false
3
Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
Time Complexity:
Ex: Consider the following algorithm to add two
numbers
𝐴𝑙𝑔𝑜_𝑎𝑑𝑑 (𝑎, 𝑏)
𝑺𝒕𝒆𝒑 𝟏. 𝑪 = 𝒂 + 𝒃;
𝑺𝒕𝒆𝒑 𝟐. 𝒓𝒆𝒕𝒖𝒓𝒏 𝑪;
Here, algorithm has only two simple statements so the
complexity of this algorithm is 2
4
Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
Example
void main ( )
{
int i , n , sum , x;
sum = 0;
printf(“Enter number of elements : ”);
scanf(“ %d” , &n);
for (i=0 ; i<n ; i++)
{
scanf(“ %d” , &x);
sum= sum + x;
}
printf(“sum = % d” , sum );
}
1. In the for loop, the condition
(i<n) will be executed (n+1)
times , and not ‘n’ times.
2. For all values of ‘i’ between 1
and n , the condition (i<n)
will evaluate to true and
when ‘i’ becomes (n+1) , the
condition (i<n) will evaluate
to false.
3. Thus the above instruction
will be executed (n+1) times.
5
Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
Calculation of computation time
Statement Frequency
sum = 0 1
printf(“Enter number of elements : ”); 1
scanf(“ %d” , &n); 1
for (i=0 1
i<n n + 1
i++ n
scanf(“ %d” , &x); n
sum= sum + x; n
printf(“sum = % d” , sum ); 1
=1 + 1 + 1 +1 + (n+1) + n + n + n + 1 =(6+4n)
6
Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
Example :
i=1;
while (i< = n)
{
x = x +1 ;
i = i + 1;
}
Statement Frequency
i=1; 1
while (i< = n) n +1
x = x +1 ; n
i = i + 1; n
=1 + (n+1) + n + n =(2+3n)
7
Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane

unit 1 SIXTH.pptx Algorithm Complexity Time

  • 1.
    SANJIVANI K. B.P. POLYTECHNIC, KOPARGAON With NBA ACCREDIATED programs , Approved by AICTE, New Delhi, Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai, ISO 9001:2015 Certified Institute Name of Faculty: Prof. Vaibhav A. Parjane 1
  • 2.
    Topic to beCovered Time Complexity 2 Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
  • 3.
    Time Complexity : 1.Time complexity is the total time taken by the algorithm to perform the intended task. 2. It is also known as frequency count. 3. Eg: consider the FOR LOOP For ( i=0 ; i<n ; i++ ) Atleast once (1) + (n+1) + n = 1 + (n+1) +n = 2n + 2 true false 3 Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
  • 4.
    Time Complexity: Ex: Considerthe following algorithm to add two numbers 𝐴𝑙𝑔𝑜_𝑎𝑑𝑑 (𝑎, 𝑏) 𝑺𝒕𝒆𝒑 𝟏. 𝑪 = 𝒂 + 𝒃; 𝑺𝒕𝒆𝒑 𝟐. 𝒓𝒆𝒕𝒖𝒓𝒏 𝑪; Here, algorithm has only two simple statements so the complexity of this algorithm is 2 4 Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
  • 5.
    Example void main () { int i , n , sum , x; sum = 0; printf(“Enter number of elements : ”); scanf(“ %d” , &n); for (i=0 ; i<n ; i++) { scanf(“ %d” , &x); sum= sum + x; } printf(“sum = % d” , sum ); } 1. In the for loop, the condition (i<n) will be executed (n+1) times , and not ‘n’ times. 2. For all values of ‘i’ between 1 and n , the condition (i<n) will evaluate to true and when ‘i’ becomes (n+1) , the condition (i<n) will evaluate to false. 3. Thus the above instruction will be executed (n+1) times. 5 Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
  • 6.
    Calculation of computationtime Statement Frequency sum = 0 1 printf(“Enter number of elements : ”); 1 scanf(“ %d” , &n); 1 for (i=0 1 i<n n + 1 i++ n scanf(“ %d” , &x); n sum= sum + x; n printf(“sum = % d” , sum ); 1 =1 + 1 + 1 +1 + (n+1) + n + n + n + 1 =(6+4n) 6 Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane
  • 7.
    Example : i=1; while (i<= n) { x = x +1 ; i = i + 1; } Statement Frequency i=1; 1 while (i< = n) n +1 x = x +1 ; n i = i + 1; n =1 + (n+1) + n + n =(2+3n) 7 Sanjivani K. B. P. Polytechnic, Kopargaon Department of Computer Technology Prof. Vaibhav A. Parjane