Question

4. [16 marks total (6 marks each)] Do a worst-case analysis for the following algorithm segments, counting the number of mult

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

4)
a)
worst case complexity:O(n^2)
explanation:
   outer loop runs for n-1 times
   inner loop runs for i+3 for each iteration of i
let i=1, inner loop runs 1+3 times
let i=2, inner loop runs 2+3 times
,
,
,
let i=n-1, inner loop runs n-1+3 times
total : 1+3 + 2+3 + 3+3 + ,,,,,, + n-1 +3 = (1+2+3+,,+n-1)+3*(n-1)= n*(n-1)/2 + 3*(n-1) = O(n^2) times
so multiplication will be performed :O(n^2)

b)
worst case complexity :O(nlogn)
outer loop runs n-2 times
inner loop runs logn times for each iteration of outer loop
total complexity : (n-2) *logn = O(nlogn)
so multiplication will be performed :O(nlogn)

c)
worst case complexity :O(n^3)
outer loop runs n times
inner middle loop runs n times
inner most loop runs n*2i times for each i of outer loop
for i=1 inner most loop runs n*2*1 times
for i=2 inner most loop runs n*2*2 times
for i=3 inner most loop runs n*2*3 times
,
,
for i=n inner most loop runs n*2*n times
total complexity : n*2*1 +n*2*2 +,,, + n*2*n = n*2*(1+2+3+,,,+n) = n*2*(n*(n+1))/2 = O(n^3)

Add a comment
Know the answer?
Add Answer to:
4. [16 marks total (6 marks each)] Do a worst-case analysis for the following algorithm segments, counting the number of multiplications which occur. I have marked the lines with the multiplications...
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