Question

1. What is the Big-Oh runtime complexity of the following algorithm? A. for (i = 5;...

1. What is the Big-Oh runtime complexity of the following algorithm?

A. for (i = 5; i <= 2 * n; i++)

    cout << 2 * n + i – 1; << enld;

2. State the preconditions &/or postcondition for each of the following.

A. ) if (x >= 0) y = x + y;

    else y = y - x;

B.) /* precondition: m <= n */

   s = 0;

   for (i = m; i <= n; i++)

       s += i;

C.) i = 1;

       c = 0;

     while (i <= n) {

       if (a[i] == 17) c = c + 1;

      i = i + 1; }

D.) m = a[1];

    i = 2;

      while (i <= n) {

           if ( a[i] > m ) m = a[i];

           i = i +1;

      }

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

1.

for (i = 5; i <= 2 * n; i++)  

  • In this i value runs for 5 to 2n.
  • The increment operation of i done linearly(Since i value incremented by 1 every time)

The Number of times loop runs is 2n-5.

f(n) = 2n-5

g(n) = 2n

By the definition of Big O

f(n) ≤ C g(n)

2n-5 ≤ 1*(2n)

Where c=1

Similarly

cout << 2 * n + i – 1; << enld; // Complexity is O(n)

Therefore the complexity of the program is O(n).

According to HomeworkLib guidelines i have to solve first question only please do next post for remaining answers.

Add a comment
Know the answer?
Add Answer to:
1. What is the Big-Oh runtime complexity of the following algorithm? A. for (i = 5;...
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