The Gateway to Computer Science Excellence
+1 vote
182 views
What is the time complexity for infinite loops

Question 1  what is T(n) for this case

While(1)

{

a=a+b;

}

 

Question 2 for this case

if(1)

{

for i to n

a=a+b

}

else

{

for i to n

for j to n

a=a+b

}

 

Edit 2: Compiled the code never goes to the else part

#include<stdio.h>
int main()
{
int a=2,b=6;
if(1)
{
a=a+b;
}
else
{
b=a;
printf("Test");
}
printf("%d %d",a,b);
return 0;
}

output I get is 8 6 which means the else case is never executed hence in worst case do we have to consider the else part.
in Algorithms by Active (2.5k points)
edited by | 182 views
0
For 1.I think there is nothing as time complexity of infinite loop, it depends on value of n you have taken.If n is infinitely large number then Time will still be O(n) though computer may crash.

2.T(n)=O(n)+O(n^2)

=O(n^2)

Please correct me if I'm wrong!
0
the else part never gets executed, so I think its O(n) for the 2nd question.
0
Yes..I missed the if condition...Thanks :)
0

And for question 1, there is no input. Since the program is in an infinite loop, there is no time complexity. And the runtime is infinite. 

0
@Soumya Tiwari I am also thinking on similar lines not sure hence want clarification.Subject experts please help.

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,370 answers
198,506 comments
105,275 users