Question

In: Computer Science

Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation...

Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation in terms of the variable n. Show your work

a) O( )

int m1(int n, int m) {

if (n < 10) return n;

else if (n < 100)

return happy (n - 2, m);

else

return happy (n/2, m);

}

-----------------------------

b) O ( )

void m2 (int n) {

j = 0;

while (j < n) {

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

{ System.out.println(”i = ” + i);

for (int k = 0; k < i; ++k)

System.out.println(”k = ” + k);

}

j = j + 1; }

}

---------------------------

c) O ( )

void m3 (int n) {

for (int i = 0; i < n * n; ++i) {

for (int k = 0; k < i; ++k)

System.out.println(”k = ” + k);

for (int j = n; j > 0; j--)

System.out.println(”j = ” + j);

}

} ------------------

d) O ( )

void m4 (int n, int x) {

for (int k = 0; k < 500; ++k)

if (x > 500) {

for (int i = 0; i < n * k; ++i)

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

System.out.println(”x = ” + x);

}

}

Solutions

Expert Solution

Time Complexity of Each function in Big-oh notation.

In order to provide clear explaination of time compexity of each statement in function,i have added comment after each statement of funtion which represent time complexity of that statement.

(a) O( )

int m1(int n, int m) {

if (n < 10) return n;  //------>0(1)=constant

else if (n < 100) //------>0(1)

return happy (n - 2, m); //------>0(1)+Time Complexity of "happy(n - 2 , m)"

else

return happy (n/2, m); //------>0(1)+Time Complexity of "happy(n - 2 , m)"

}

Total time complexity = 0(1) + 0(1) + 0(1)+Time Complexity of "happy(n - 2 , m) + 0(1)+Time Complexity of "happy(n - 2 , m)

m1() = 0(1)+Time Complexity of "happy(n - 2 , m) (ANSWER)

Note:- We can not determine time complexity of function m1 in terms of variable n unless we know time complexity of funtion happy().

(b) O( n3 )  

void m2 (int n) {

j = 0; //------>0(1)

while (j < n) { //------>0(n)

for (int i = 0; i < n; ++i) //------>0(n2)

{ System.out.println(”i = ” + i);   //------>0(1)

for (int k = 0; k < i; ++k) //------>(1+2+3+......+n)*n = (n(n+1)/2)*n = 0(n3)

System.out.println(”k = ” + k); //------>0(1)

}

j = j + 1; } //------>0(1)

}

There are 2 "for" loop.For every iteration of "outer for" loop "inner for" loop is execute.

Total time complexity of function m2() = 0(1)+0(n)+0(n3)+0(1)

m2() = O ( n3 ) (ANSWER)

(c) O( n4 )  

void m3 (int n) {

for (int i = 0; i < n * n; ++i) { //------>0(n2)

for (int k = 0; k < i; ++k) //------>(1+2+3+......+n2) = n2(n2+1)/2 = 0(n4)//For every outer for loop this loop is executed.

System.out.println(”k = ” + k); //------>0(1)

for (int j = n; j > 0; j--)) //------>0(n)

System.out.println(”j = ” + j); //------>0(1)

}

} ------------------

Total time complexity of function m3() = 0(n2)+0(n4)+0(1)+0(n)+0(1)

m3() = O ( n4) (ANSWER)

(d) O ( n2 )

void m4 (int n, int x) {

for (int k = 0; k < 500; ++k) //------>0(1)

if (x > 500) {

for (int i = 0; i < n * k; ++i) //------>0( n )

for (int j = 0; j < n; ++j) //------>0( n2 )

System.out.println(”x = ” + x); //------>0(1)

}

}

Total time complexity of function m4() = 0(1)+0(n)+0(n2)+0(1)

m4() = O (n2) (ANSWER)


Related Solutions

Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2....
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2. 100nlog n + n5 + 100n 3. n2log n + nlog n 4. 2n + n5 + 5n 5. 100n + 0.01n2 A. O(n3) B. O(5n) C. O(n2) D. O(n5) E. O(n2log n)
42. Describe the order of growth of each of the following functions using O notation. a....
42. Describe the order of growth of each of the following functions using O notation. a. N2+3N b. 3N2+N c. N5+100N3+245 d. 3NlogN+N2 2 e. 1+N+N2 +N3 +N4 f. (N * (N − 1)) / 2 Describe the order of growth of each of the following code sections, using O notation: count = 0; for (i = 1; i <= N; i++) count++; count=0; for (i = 1; i <= N; i++) for (j = 1; j <= N; j++)...
For each of the following six program fragments give an analysis of the running time (Big-Oh...
For each of the following six program fragments give an analysis of the running time (Big-Oh will do). (25,very 5 mark) (a)sum = 0; for( i=0; i<n; i++ ) for( j=0; j<n; j++ ) sum++; (b) sum = 0; for( i=0; i<n; i++ ) for( j=0; j<n*n; j++ ) sum++; (c) sum = 0; for( i=0; i<n; i++ ) for( j=0; j<i; j++ ) sum++; (d) sum =0; for( i=1; i<n; i++ ) for( j=1; j<i*i; j++ ) if( j%1...
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
What is the Big O of the following algorithms along with the worst and average cases:...
What is the Big O of the following algorithms along with the worst and average cases: Euclid's Algorithm Brute-Force Matching Topological Sort Lomuto Partition Russian Peasant Algorithm
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]; } } }
Please state the worst case run time for the following with an example of the worst...
Please state the worst case run time for the following with an example of the worst case and explain why! 1. Dijksta's Algorithm 2. Bellman-Ford Algorithm 3.DAG Algorithm 4. Prim's Algorithm 5. Kruskal's Algorithm 6. Baruvka's algorithm
For the problem below, please estimate the worst-case Running Time for a random array of size...
For the problem below, please estimate the worst-case Running Time for a random array of size n, and prove that it is indeed ( ). Please show all your work. Just stating something like "since 2 Binary Search runs in ( ) time, our algorithm has the same runtime estimate" is not enough. A rigorous explanation is expected. import java.util.*; public class Main { static int binarySearch(int arr[], int l, int r, int x) { if (r >= l) {...
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can...
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can be extended to functions in more than one variable. For example, the statement f(x, y) is O(g(x, y)) means that there exist constants C, k1, and k2 such that|f(x, y)|≤C|g(x, y)|whenever x > k1 and y > k2] 2. Find a div b and a mod b when: (a) a = 30303, b = 333 (b) a = −765432, b = 3827 3. Convert...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer. 1a int a = 0, b = 0; for (int i = 0; i< N; i++) {     a = a + i; } for (int j = 0; j < N; j++) {     b = b + j; } 1b bool isPrime(int N) { if (N == 1) return false; for (x = 2; x <= sqrt(N);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT