Question

PART B- The Efficiency of Algorithms, 10 points 1. Show how you count the number of operations (not only . Consider the follo

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

1.

sumList(aList, n) {

thisSum = 0; //runs O(1) time

lastSum = 5000; //runs O(1) time

for(int i=0; i<=n; i++) { //runs O(n+1) times

thisSum = thisSum + aList[i] * 2; //runs O(n+1) times

}

return thisSum >= lastSum; //runs O(1) time

}

In total => O(1 + 1 + (n+1) + (n+1) + 1) time

Remove the constant times, which gives the overall time complexity of O(n) time complexity.

2.

Loop A runs n*10000 times. If n = 100, then it will execute 100*10000 times, which in total 1000000 times.

On the other hand, Loop B runs n*n times. If n = 100, then it will execute 100*100 times, which in total 10000 times.

As you can see, Loop B is faster than Loop A if and only if, n is less than 10000. If n is greater than 10000 then loop A runs faster.

Add a comment
Know the answer?
Add Answer to:
PART B- The Efficiency of Algorithms, 10 points 1. Show how you count the number of...
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