Question

Compute the time complexity for each of the following two program fragments with respect to N....

Compute the time complexity for each of the following two program fragments with respect to N. Show your steps in reaching your answer.

1)             for(i=1; i < N; i = i*2) {

      for(j=0;j

            // Operations with constant time…

      } }

2)              for(i = 0; i < sqrt(N); i++){

      for(j=1; j < i+8; j++){

           for(k=0;k

                  // Operations with constant time…

           } } }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

it will be nlogn.

for the first loop it is multiplication by i and second loop in j is dependent on i and will run for n times as well so it will be nlogn.

2)

it should be roughly n2 as the second first and second loop are dependent and will run together for roughly n times and the k for will run for n more times so n2

Add a comment
Know the answer?
Add Answer to:
Compute the time complexity for each of the following two program fragments with respect to N....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1- Find the time complexity of the following program, where n is given as input: i...

    1- Find the time complexity of the following program, where n is given as input: i = n; while (i > 1) { j = i; while (j < n) { k = 0; while (k < n) { k += 2; } j *= 2; } i /= 2; } Express your answer using theta notation, and explain the amount of time it takes for each loop to finish.

  • For each of the following six program fragments: a. Give an analysis of the running time...

    For each of the following six program fragments: a. Give an analysis of the running time (Big-Oh will do). b. Implement the code in the language of your choice, and give the running time for several values of N. Pseudo Code Implementation Analysis of runtime time (Big-Oh) (1) sum = 0; for(i = 0; i < n; ++i) ++sum; (2) sum = 0; for(i = 0; i < n; ++i) for(j = 0; j<n; ++i) ++sum; (3) sum = 0;...

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • Show the output produced by each of the following program fragments. Assume that I, j, k...

    Show the output produced by each of the following program fragments. Assume that I, j, k are int variables i = 7; j = 2; printf(“%d %d ”, i / j, i % j); i = 4; j = 3; printf(“%d”, (i + 10 ) % j); i = 7; j = 8; k = 9; printf("%d”, (i + 10) % k / j); Show the output produced by each of the following program fragments. Assume that I, j, k...

  • For each of the following two program fragments: a. Give an analysis of the running time...

    For each of the following two program fragments: a. Give an analysis of the running time (Big-Oh will do). (1) std::vector<int> my_vect; for( i = 0; i < n; i++ ) my_vect.insert(0, i); (2) std::vector<int> my_vect; for( i = 0; i < n; i++ ) my_vect.push_back(i); (3) std::list<int> my_list; for( i = 1; i < n; i++ ) my_list.insert(i, 0); (4) std::list<int> my_list; for( i = 1; i < n; i++ ) my_list.push_front(i);

  • Question 1 (25 pts) Find the running time complexity for the following code fragments. Express yo...

    Question 1 (25 pts) Find the running time complexity for the following code fragments. Express your answers using either the Big-O or Big-Θ notations, and the tightest bound possible. Justify your answers. for(int count O , i -0; i < n* n; i++) for(int i0 ; j <i; j++) count++ for(int count O , i -0; i

  • Question 3: Given the following two code fragments [2 Marks] (i)Find T(n), the time complexity (as...

    Question 3: Given the following two code fragments [2 Marks] (i)Find T(n), the time complexity (as operations count) in the worst case? (ii)Express the growth rate of the function in asymptotic notation in the closest bound possible. (iii)Prove that T(n) is Big O (g(n)) by the definition of Big O (iv)Prove that T(n) is (g(n)) by using limits

  • discrete math (1) (15 pts) Time Complexity Analysis 1) (5 pts) What is the time complexity...

    discrete math (1) (15 pts) Time Complexity Analysis 1) (5 pts) What is the time complexity of the following code segment? Explain your answer; otherwise, you can't get full mark from this question. for(int i=1; i<n; i*=2) { sum-0; sum++; Answer: 2) (5 pts) What is the time complexity of the following code segment? Explain your answer; otherwise, you can't get full mark from this question. for(int j=0; j<n; j++){ for (int k=0; k<n; k++) { for (int =0; i<n;...

  • Data Structures and Algorithms For each of the following program fragments, give an analysis of the...

    Data Structures and Algorithms For each of the following program fragments, give an analysis of the running time using Big-Oh notation. Do not give formulas, but analyze every line to calculate the running time, e.g. sum = 0 is equal to 1 unit time ... b. sum = 0;    for( i = 0; i < n; i++)            for( j = 0; j < i*i; j++)                  for( k = 0; k < j; k++)                                           sum++; c. sum =...

  • Show your work Count the number of operations and the big-O time complexity in the worst-case...

    Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT