Question

What is the time complexity of the following code? (in big-O notaion) sum = 0 count...

What is the time complexity of the following code? (in big-O notaion)
sum = 0

count = 0

var1 = 0

for: i=1 to n{

sum = sum+i

for j=1 to [(3i+2i)(5n)(2i)]{

var1 = sum+count}

}

for m = i to j*j{

sum = sum*sum

var2 = sum - (count+3)}

Homework Answers

Answer #1

As we know that big O notation is a fundamental tool to compute the complexity in mathematical term.

so formula would be f(x)<= cg(x) where c is constant , it could be any constant. for example

f(x)= 2x2 +3x + 5 <= 20x2 +33x + 5

                               <= 20x3 +any thing

                               <= 20x4 +33x + anything all would be BigO notation

so now i am comming to the point of question, i have devide whole code in line so that during the interpretation it would be easy to understand.

see the carefully code line 4 it would be go 1 to n , if here i take n=5 the 1 to 5 so line 5 will take constant time when we talk about the big number for n then it would be negligible, now calculate theline 6 and line 9 to 11, normally if we understand of line 6 see

Here third loop is independent , and i and j is defined local variable, if we assume to both varibale independent, so m=i go j*j if j = n then it will go n2 so so now we can write as assemble the whole thing :

c(constant)+n3 + n2 +c(constant) =O(c*n3)

i hope you got the main thing

Know the answer?
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
Similar Questions
What is the tilde time complexity for the following code fragments: 1) int sum = 0;...
What is the tilde time complexity for the following code fragments: 1) int sum = 0; for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) sum++; 2) int sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < i; j+=2) sum++;
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 =...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 = 0; sum5 = 0; for(i=1; i<=n/2; i++) { sum2 = sum + 2; } for(j=1; j<=n*n; j++) { sum5 = sum + 5; } i think it is O(n^2) since big oh finds the order of worst case which is the the second loop being n^2 complexity. but im not sure. 2.) Give an efficient algorithm along with running time analysis to find the...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Show that the running time for the following segment of code is O(N3) without using the...
Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to count each statement that takes one unit of time. sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ )             for( k = 0; k < n; k++ ) sum++;
Analyze the following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for ( int i = n; i > 0; i /= 2 ) { for ( int j = 1; j < n; j += 2 ) { for ( int k = 0; k < n; k += 2 ) { ... // constant number of operations } } }
Analyze the following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for (int i = n; i >= n; i = i/2)           some O(1) time statements; end for
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
2. Write the big-O expression to describe the number of operations required for the following two...
2. Write the big-O expression to describe the number of operations required for the following two pieces of code. You need to explain your solution and show your work. (3 points) code 1: counter = 0       for (i = 1; i < n; ++i) {           for (j = 1; j < i; j++) { counter++; }    } code 2: counter = 0; for (i = 1; i < n; ++i) { for (j = 1; j <...
Given the recursive bubble sort algorithm, analyze its time complexity in terms of Big O. bubbleSort(A[],...
Given the recursive bubble sort algorithm, analyze its time complexity in terms of Big O. bubbleSort(A[], n) { // Base case if (n == 1) return;    // One pass of bubble sort. After // this pass, the largest element // is moved (or bubbled) to end. for (i=0; i<n-1; i++) if (A[i] > A[i+1]) swap(A[i], A[i+1]);    // Largest element is fixed, // recur for remaining array bubbleSort(A, n-1); }
Answer the following questions: 1. Given the code segments below with n as the problem size,...
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...