Question

Calculate the running time for the following code fragment. Show your work. for (i:= 0; i...

Calculate the running time for the following code fragment. Show your work.

for (i:= 0; i < n; i++)

{

for (j:=1; j < n; j*=2)

     {

counter := counter +1

                }

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Generally one loop nested in another will have O(n²).

in this cae j=j*2 is the pattern for j for firs for loop wil have to expcute n time where as inner for loop will be executed half than n as j =j*2.

So i will be (n*n)/2

Time complexity will be O(n^2/2) .

Let me know if you have doubts.

Add a comment
Know the answer?
Add Answer to:
Calculate the running time for the following code fragment. Show your work. for (i:= 0; i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT