+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.

edited | 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.