Question

In: Computer Science

Q)Determine the complexity of the following code chunks. Show all calculations. (Assume that f() has complexity...

Q)Determine the complexity of the following code chunks.

Show all calculations. (Assume that f() has complexity O(1)).

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

B) for(i=0; i<n; i++)

                  for(j=0; j<n; j++)

                            sum[i] += entry[i][j];

C) for(i=0; i<n; i++)

                  for(j=0; j<i; j++)

                            m += j;

D) i= 1;

while(i< n)

{ tot += i;

i= i* 2;}

E) i= n;

while(i> 0) {

tot += i;

i= i/ 2; }

F) for(i=0; i<n; i++)

                 for(j=0; j<n; j++)

                               for(k=0; k<n; k++)

                                       sum[i][j] += entry[i][j][k];

G) for(i=0; i<n; i++)

                    for(j=0; jn; j++)

                                 sum[i] += entry[i][j][0];

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

                 for(k=0; k<n; k++)

                        sum[i] += entry[i][0][k];

H) for(i=0; i<n; i++)

                 for(j=0; j< sqrt(n); j++)

                                     m += j;

  1. for(i=0; i<n; i++)

         for(j=0; j< sqrt(995); j++)

                      m += j;

J) int total(int n)

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

                  subtotal += i;

main()

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

tot += total(i);

K) for(i=0; i<n;i++) {

subtotal = 0;

for(j=0; j < i; j++)

                subtotal += j;

tot += subtotal;}

Solutions

Expert Solution

a) O(n)

The loop is executed n times and addition takes O(1) times.

===============

b) O(n^2)

There are two nested loops. For each value of i, j is executed n times. Also i loop is executed n times. The loop body takes constant time. So total of n^2 times loop body is executed. Hence time complexity = O(n^2)

===============

c) O(n^2)

Total no. of times loop body is executed = 0 + 1 + 2 + .. n-1 = n*(n-1)/2 = O(n^2)

================

d) O(logn)

The loop runs for i = 1,2,4,8,.. 2^k. So it runs for k times where 2^k <= n but 2^(k+1) >n.

So 2^k = n.

Hence k = logn.

Hence time complexity = O(logn)

==================

e) O(logn)

Loop runs for i = n,n/2, n/4,n/8... n/2^k

Loop terminates when n/2^k < 1

HEnce k = logn times loop runs.

===================

f) O(n^3)

Three nested loops each running n times.

=======================

g) O(n^2)

The first and second nested loop runs n^2 times both. So total of 2n^2 times. Which is O(n^2)

=======================

H) O(n1.5)

The outer loop runs n times and for each value inner loop runs sqrt(n) times. Hence n*n^0.5 = n1.5 times.

======================

I) O(n)

It runs for sqrt(995)*n = 31n times approx.Hence O(n)

=====================

J) O(n^2)

Total function runs for n times.

It is called from main for values 0,1,2.. n-1 times. So n*(n-1)/2 times subtotal+=i; statement is run. Hence O(n^2)

=======================

K) O(n^2)

Similar to above part J. But within nested loop only.

========================

Please upvote.


Related Solutions

Please show all work and calculations neatly! no code allowed! --------------- Using Simpson's Rule, verify that...
Please show all work and calculations neatly! no code allowed! --------------- Using Simpson's Rule, verify that the weight w1 equals 4h/3 by integrating the appropriate Lagrange basis function
Show that if F is a finite extension of Q, then the torsion subgroup of F*...
Show that if F is a finite extension of Q, then the torsion subgroup of F* is finite
(Show All the Calculations /Wherever Necessary Draw Diagrams) Q 1. An electromagnetic wave with its electric...
(Show All the Calculations /Wherever Necessary Draw Diagrams) Q 1. An electromagnetic wave with its electric field parallel to the plane of incidence is incident from vacuum onto the surface of a perfect conductor at an angle of incidence θ. Obtain an expression for the total electric and the magnetic field.
determine the distances between the genes, coefficient of coincidence, and interference. Show all calculations. (create pairs...
determine the distances between the genes, coefficient of coincidence, and interference. Show all calculations. (create pairs eg wfm and +++ are a parental pair) Phenotype Number of Individuals wfm 5 +++ 7 +fm 4 w++ 0 w+m 2 +f+ 1 wf+ 0 ++m 2
Directions: Answer ALL of the following questions. Show ALL calculations and present them in a neat...
Directions: Answer ALL of the following questions. Show ALL calculations and present them in a neat and an orderly fashion. Credit will not be given for either unsupported answers or illegible answers. This is NOT a group assignment. Any indication of collaboration with your classmates will earn all involved a grade of zero (0). This assignment is due Monday, October 5, 2020. However, you may submit it before the due date. Late assignments are NOT accepted. The following is information...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
Show manual calculations to illustrate how the following operations would be performed in MATLAB. Determine the...
Show manual calculations to illustrate how the following operations would be performed in MATLAB. Determine the result and the size of the following expression. Note that the later arrays may depend on the definition of earlier arrays (a) a = 6:-3:-4 (b) b = [ a’ a’ a’] (c) c = b(1:2:3, 1:2:3) (d) d = b(3:-2:1,: ) (e) w = [zeros(1,3) , ones(3,1)’ , 3:5’]
Let p and q be propositions. (i) Show (p →q) ≡ (p ∧ ¬q) →F (ii.)...
Let p and q be propositions. (i) Show (p →q) ≡ (p ∧ ¬q) →F (ii.) Why does this equivalency allow us to use the proof by contradiction technique?
. YOU MUST SHOW ALL CALCULATIONS TO EARN CREDIT.                                   &nbsp
. YOU MUST SHOW ALL CALCULATIONS TO EARN CREDIT.                                                                         2016                            2017 BALANCE SHEETS: Assets:                         Cash                                        120,000                       160,000                         Accounts Receivable               520,000                       620,000                         Inventory                                305,000                       290,000                         Fixed Assets, net                    410,000                       510,000                         Total Assets                           1,355,000                    1,580,000 Liabilities and Equity:                         Accounts Payable                   350,000                       $375,000                         Long-term Debt                      500,000                       625,000                         Common Stock                       50,000                         75,000                         Retained Earnings                   455,000                       505,000                         Total Liabilities and Equity    1,355,000                   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT