Question

hi show your solution in full details not just the final answer ,cheers mate can you...

hi show your solution in full details not just the final answer ,cheers mate

can you help

please

thanks I am stuck please

Answer the following questions:

  1. Given the code segments below with n as the problem size, answer the following questions:

//Code Segment 1 (Consider n as a power of 3)

int sum = 0;

for(int i = 1; i <= n; i = i*3)               
sum++;
                                   // statement1

//Code Segment 2: (Consider both possibilities for x)

if(x < 5){

    for(int i = 1; i <= n; i++)              
      for(int k = 1; k <= i; k++)        
       sum++;                        // statement2

}

else{

     for(int i = 1; i <= n; i++)

           for(int j = 1; j <= i; j++)

                for(int k = 1; k <= j; k++)

         sum++;               // statement3

}

//Code Segment 3: Consider n as a power of 4.

for(int i = 1; i <= n; i = i*4)                     
for(int k = 1; k <= i; k++)                
sum++;                             // statement4

  1. (48 points) Find the exact number of times statement1, statement2, statement3 and statement4 get executed in terms of n.

(12 points) Determine the Big-O complexity of these 3 program fragments.

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

Answer:

//Code Segment 1 (Consider n as a power of 3)

int sum = 0;

for(int i = 1; i <= n; i = i*3)           (Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount and here the loop variable is multiplied by 3.)
sum++;
                                    // statement1 (So statement 1 is executed log3n times and time complexity will be O(log3n).)

//Code Segment 2: (Consider both possibilities for x)

if(x < 5){

    for(int i = 1; i <= n; i++) (Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. So this loop will run for n time.)
     for(int k = 1; k <= i; k++)   (This is inner loop and will run for n time because it will run till k<=i).
       sum++;                        // statement2 (So statement 2 is executed n*(n+1)/2 time and time complexity will be O(n*n).)

}

else{

     for(int i = 1; i <= n; i++) (This will run for O(n) time.)

           for(int j = 1; j <= i; j++) (This will run for O(n) time.)

                for(int k = 1; k <= j; k++)   (This will run for O(n) time.)

         sum++;               // statement3 (So statement 3 is executed n(n+1)(2n + 4)/12 times.and time complexity will be O(n*n*n).)

}

//Code Segment 3: Consider n as a power of 4.

for(int i = 1; i <= n; i = i*4)                      (Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount and here the loop variable is multiplied by 4.)
for(int k = 1; k <= i; k++) (This will run for O(n) time, because of inner loop.)
sum++;                             // statement4 (So statement 4 is executed n * log4n times and time complexity will be O(n*log4n).)

Thus, we conclude that,

a)

Statement 1 is executed log3n times.

Statement 2 is executed n*(n + 1)/2 times.

Statement 3 is executed n(n+1)(2n + 4)/12 times.

Statement 4 is executed n * log4n times.

b)

Time complexity of code segment 1 is: O(log3n).

Time complexity of code segment 2 is: O(n3).

Time complexity of code segment 3 is: O(n * log4n).

Please give thumbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
hi show your solution in full details not just the final answer ,cheers mate can you...
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
  • discrete math (1) (15 pts) Time Complexity Analysis 1) (5 pts) What is the time complexity...

    discrete math (1) (15 pts) Time Complexity Analysis 1) (5 pts) What is the time complexity of the following code segment? Explain your answer; otherwise, you can't get full mark from this question. for(int i=1; i<n; i*=2) { sum-0; sum++; Answer: 2) (5 pts) What is the time complexity of the following code segment? Explain your answer; otherwise, you can't get full mark from this question. for(int j=0; j<n; j++){ for (int k=0; k<n; k++) { for (int =0; i<n;...

  • Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question...

    Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question (Big O): Please explain me in details the order of growth (as a function of N) of the running times of each of the following code fragments: a)         int sum = 0; for (int n = N; n > 0; n /= 2)    for(int i = 0; i < n; i++)      sum++; b)         int sum = 0; for (int i =...

  • Show your work Count the number of operations and the big-O time complexity in the worst-case...

    Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • please help me on 12 106 Java Concepts Advanced Placement CS Study Guide 3 12. Consider...

    please help me on 12 106 Java Concepts Advanced Placement CS Study Guide 3 12. Consider the following code segment. int count = 0; for (int x = 0; x< 3; x++) for (int y = x; y < 3; y++) for (int z - y: z < 3; 2++) count++; What is the value of count after the code segment is executed? a 81 b. 27 JOHNNNN mshe thote c. 10x d. 9 e, 6 13. Each segment of...

  • 1. (25 points) Assume each expression listed below represents the execution time of a program Express...

    1. (25 points) Assume each expression listed below represents the execution time of a program Express the order of magnitude for cach time using big O notation. a. T(n) = ns + 100n . log2 n + 5000 b. T(n) = 2" +n” + 7 d. T(n) 1+2+4+2 2 (75 points+5 extra credit) For cach of the code segments below, determine an equation for the worst-case computing time Tn) (expressed as a function of n, ie. 2n+4) and the order...

  • Please answer the question (d), (e), (f). 11. (12 points, 2 points each) Find the computational...

    Please answer the question (d), (e), (f). 11. (12 points, 2 points each) Find the computational complexity for the following code fragments: (a Nyhoff p. 364 Drozdek pp. 72-73, f Weiss, p. 72) a. for (int x = 1, count = 0, 1-0; i < n; i++) for (int j 0; j <= x; j++) counttti x = 2; b. for (int count 0, i = 0; i < n; i++) for (int j = 0; j < n; j++)...

  • Just the correct answer only please please 37. In an ascending sorted List named myVals the...

    Just the correct answer only please please 37. In an ascending sorted List named myVals the kth smallest item is found by which of the following? myVals [ k ] myVals [ k-1 ] myVals [ numItems - k ] myVals [ numItems + k ] 38. What is the Time Complexity of the following snippet of code? for ( int i = 0; i < n; ++i){ int j = 1; while(j < n ){ j = 2 *...

  • 1. Determine the appropriate big-o expression for each of the following functions, and put your answer...

    1. Determine the appropriate big-o expression for each of the following functions, and put your answer in the table we have provided in section 2-1 of ps5_parti. We've included the answer for the first function. (Note: We're using the “ symbol to represent exponentiation.) a (n) = 5n + 1 b. b(n) = 5 - 10n - n^2 o c(n) = 4n + 2log (n) d. e. d(n) = 6nlog (n) + n^2 e(n) = 2n^2 + 3n^3 - 7n...

  • What is the output from each of the following segments of C++ code? Record the output...

    What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 25; j > 16; j -= 3)        cout << setw(5) << j;     Answer: 2. int sum = 0;       for (int k = -2; k <= 2; k++)         sum = sum +...

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