What is the time complexity T(n) of the nested loops
below? For simplicity, you may assume that n is a power of
2. That is, n = 2k for some positive integer k.
:
i = n;
while (i >= 1){
j = i;
while (j <= n) {
<body of the inner while loop > //
Needs (1).
j = 2 * j;
}
i = i/2;
}
:
Answers were Sorted based on User's Feedback
Answer / arefe
hello I'm not sure about what I'm going to say . please tell me if you think it's wrong.
at first let's write some steps of it .
1) i = j = n
2) i = n/2 , j = n/2 --> j = n
3) i = n/4 , j = n/4 --> j = n/2 --> j = n
4) i = n/8 , j = n/8 --> j = n/4 --> j = n/2 , j = n
.
.
.
now if we count ,
the number of n , which j is equal to is log(n) ,
the number of n/2 , which j is equal to is log(n)-1 ,
the number of n/4 , which j is equal to is log(n)-2 ,
the number of n/8 , which j is equal to is log(n)-3 ,
and so on ,
so the result is summation log(n) - i , which i is from 0 to log(n) ,
we write log(n) out of Sigma and multiple it with log(n) + 1,( if we write Sigma term we have log(n) +1 , log(n))
and summation i , which i is from 0 to log(n) , is equal to (log(n)+1)(log(n))/2
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / muhammad ijaz khan
The outer loop divides the working area in half in each
iteration. So the running time of this algorithm is
proportional to the number of times n can be divided by 2.
The inner loop will be executed unlimitted time. So
the time complexity of the outer loop will be the function
of ln and the total time is: T(n)= n*ln n
| Is This Answer Correct ? | 10 Yes | 20 No |
what in 4th and 5th normal form ??
what are macros?what are its advantages and disadvantages in c language?
What is noise figure?
UST RIL MAR BER UNE ARY ?Choose the odd one out
Which of the following highest Standard deviation? a).5,0,5,0,5,0 b) 5,5,5,5,5,5 c) 5,5,5,5,0,5 d) -5, 5, -5, 5,-5, 5
friends help me..am a btech fresher.. wt is the eligibility criteria for TCS???? like %...repli me clearly from x onwards..
Tell me about a major problem you recently handled. Were you successful in resolving it? in petroleum side ?
what is difference between class and object in c++
main() { int y=10; if(y++>9 && y++!=10 && y++>10) { printf("%d",y); else printf("%d",y); } }
Explain about a light bulb
Can one be shocked holding a wire carrying a very large current with a low voltage source?
what are the advantages as disadvantages of the V-model?
Civil Engineering (5086)
Mechanical Engineering (4456)
Electrical Engineering (16639)
Electronics Communications (3918)
Chemical Engineering (1095)
Aeronautical Engineering (239)
Bio Engineering (96)
Metallurgy (361)
Industrial Engineering (259)
Instrumentation (3014)
Automobile Engineering (332)
Mechatronics Engineering (97)
Marine Engineering (124)
Power Plant Engineering (172)
Textile Engineering (575)
Production Engineering (25)
Satellite Systems Engineering (106)
Engineering AllOther (1379)