Question

In: Computer Science

Analyze the following codes by computing their running time, T(n). 1) int function ( int n)...

Analyze the following codes by computing their running time, T(n).

1)

int function ( int n) {
int sum;
for (int i = 0; i < n; i++)
​ sum = sum + (i * n);
return sum;
}


2)

int function(int n) {
​int sum;
​for (int i = 0; i < n; i++)
​​if (n < 1000)
​​​sum++;
​​else
​​​sum += function(n);
}

3)

int function(n)
{
​int s = 0;
​for (int i = 1; i <= n*n*n; i++)
​{
​​for (int j = 1; j <= i; j++)
​​{
​​​s = s + j;
​​}
​}
​return s;
}

Solutions

Expert Solution

1)

int function ( int n) {

int sum; // takes O(1) time

for (int i = 0; i < n; i++) // this loop runs n times for i = 0 to n-1

​ sum = sum + (i * n); // this statement is executed n times

return sum; // takes O(1) time

}

Hence, the time complexity of this code snippet is O(n).

2)

int function(int n) {

​int sum; // takes O(1) time

​​for (int i = 0; i < n; i++) // this loop runs n times for i = 0 to n-1

​​if (n < 1000) // takes O(1) time

​​​sum++; // takes O(1) time

​​else

​​​sum += function(n);   // takes O(n) time

}

Hence, the time complexity of the above algorithm is:

= O(n*1000) + O(n * (n-1000))

= O(n2)

3)

int function(n)

{

​int s = 0; // takes O(1) time

​for (int i = 1; i <= n*n*n; i++) // this loop runs n3 times for i = 1 to n3

​{

​​for (int j = 1; j <= i; j++) // this loop runs i times for each i

​​{

​​​s = s + j;

​​}

​}

​return s;

}

Hence, the total number of times the statement ​​​s = s + j; is executed is:

= 1 + 2 + 3 + ..... + n3

= n3(n3 + 1)/2 = O(n6)


Related Solutions

What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
a) Let T(n) be a running time function defined as T(n) = 3n^2 + 2n +...
a) Let T(n) be a running time function defined as T(n) = 3n^2 + 2n + 5, is this ϴ(n^2 )? Explain prove your answer using the definitions of big-o and omega notations. b) Solve the following recurrence relations using Master theorem. a. ?(?) = 3? ( ?/3 ) + ? b. ?(?) = 5?( ?/2 ) + 2?^2 please help them with both
For the following program fragment, (1) give an analysis of the running time T(n) as a...
For the following program fragment, (1) give an analysis of the running time T(n) as a function of n and (2) give a big-O bound on the asymptotic running time (tight bounds for full credit). Sum = 0; for (i=0; i< n; i++)    for(j=0; j < i*i; j++) if(j % i == 0) for (k=0; k<j; k++) Sum = Sum + 1;
Find the runtime of this function, where n is an integer. int function(int n) { int...
Find the runtime of this function, where n is an integer. int function(int n) { int a = 0; for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { a = a + i + j; } } return a; } Find the runtime of this function, where m and n are two integers. int f(int m, int n) { if (m==1 || n==1) { return 1; } return f(m,...
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
Problem 10 Write down the running time function, T(n), of the program below. def prob10(L): s...
Problem 10 Write down the running time function, T(n), of the program below. def prob10(L): s = 0 for x in L: for y in L: if x+y % 10 == 0: print(x, y, x+y) s = s + x*y return s ​ ANSWER: T(n) = ... ***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive functions,  Substitution method & Master theorem Please answer within these topics.***
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L)...
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L) <= 1: return 0 output = 0 for x in L: for y in L: output += x*y for x in L: output += x left = L[ 0 : len(L)//2 ] right = L[ len(L)//2 : len(L) ] return output + prob15(left) + prob15(right) ​ ANSWER: T(n) = ... ***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT