Question

In: Computer Science

For each of the following program fragments, give an analysis of the running time (Big-Oh will...

For each of the following program fragments, give an analysis of the running time (Big-Oh will do).

Give the exact value leading to the Big-Oh conclusion, and show your work.

sum = 0; //1

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

++sum;

sum = 0; //2

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

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

++sum;

sum = 0; //3

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

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

++sum;

sum = 0; //4

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

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

++sum;

sum = 0; //5

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

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

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

++sum;

Solutions

Expert Solution

/*1. O(n) is time complexity for the given program.
because the value of 'i' ranges from o to (n-1) and sum will be n for i=(n-1).
*/

sum = 0;
for( i = 0; i < n; ++i )
++sum;
/*2. O(n^2) is time complexity for the given program.
for every value of i(ranges 0 to n-1), j ranges from(0 to (n-1)).
*/

sum = 0; //2
for( i = 0; i < n; ++i )
for( j = 0; j < n; ++j )
++sum;
/*3. O(n^3) is the time complexity for the given program.
for every value of i(ranges from 0 to n-1), j will range from 0 to n^2.
*/

sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < n * n; ++j )
++sum;
/*4. O(n^2) is the time complexity of given program.
for each vaue of i(ranges from o to (n-1)), j will range from o to (i-1).
No. of times inner loop gets evaluated is 1+2+...+(n-1)=(n-1)*n/2.
*/

sum = 0;
for( i = 0; i < n; ++i )
for(j = 0; j < i; ++j )
++sum;
/*5. O(n^4) is the time complexity of the given program.
For each value of i(ranges from 0 to (n-1)), j ranges 0 to i^2 and k ranges from 0 to j;
for i=n, second loop(j ranges from 0 to n^2) and the third loop(k ranges also from 0 to n^2).
(n-1)^2*(n-1)^2
*/

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


Related Solutions

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...
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]; } } }
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;
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;...
1. Give, using “big oh” notation, the worst case running times of the following procedures as...
1. Give, using “big oh” notation, the worst case running times of the following procedures as a function of n ≥ 0. (a). procedure unknown for i =1 to n – 1 do for j =i + 1 to n do for k =1 to j do {statements requiring O(1) time} endfor endfor endfor (b). procedure quiteodd for i =1 to n do if odd(i) then for j =i to n do x ← x + 1                            endfor                           ...
2 algorithms for Prefix Averages, one that ran in big-Oh n2 time and a second that...
2 algorithms for Prefix Averages, one that ran in big-Oh n2 time and a second that ran in big-Oh n time. Code up methods for both algorithms. Show through different input examples and using the Current Time method call how the polynomial time algorithm runs slower than the linear time version. Use system.out.println statement to show your results. please who can help with this question In Java Thank you
Purpose This project is meant to give you experience with sorting, binary searching, and Big-Oh complexity....
Purpose This project is meant to give you experience with sorting, binary searching, and Big-Oh complexity. Objective "Write in Java please" Your goal is to take a book, as a large text file, and build a digital “concordance”. A concordance is like an index for a book in that it contains entries for words in the book, but it also includes passages from the book containing that word. For example, a query for the word Dormouse in Alice in Wonderland...
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for...
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for the following code: Part a       (hint: make use of sum of geometric progression): for (int i=1; i <= n ; i = i*2) {           for ( j = 1 ; j <= i ; j ++)             {                       cout<<”*”;             } } Part b      (hint: make use of sum of square of a number sequence): for (int i=1; i <= n ; i...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT