Question

Analyze the running time of the following algorithms asymptotically. (a) Algorithm for loop(n): for i 1 to 5n2 do papxi return p (b) Algorithm for loop for i 1 to n do for ito n do s S i return s (c) Algorithm WhileLoop(n): j 2; while G n) x x 1 j j x 2,

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

(a) It simply run 5*n2 times . Hence, its time complexity is O(n 2)

(b) There are two nested for loops. for each outer contains variable i, inner loop will run n-i times.

hence it runs n + n-1 + n-2....1 = n*(n+1)/2 times.

Hence , its time complexity is  O(n 2)

(c) Here the value of j varies from

2,4,8....n

Hence it will run log2 n -1 times. Hence, its time complexity is O(log2 n) times

Add a comment
Know the answer?
Add Answer to:
Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for...
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