Question

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);

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

Note:
   the complexities of push_back and push_front is O(1)
   and for insert() is O(n+m)//where n is number of elements inserted and number of elements moved
now
(1) std::vector<int> my_vect;

for( i = 0; i < n; i++ ) //runs n times
my_vect.insert(0, i);//inserts i at position 0//its worst case here is O(n)
total complexity : O(n*n)

2) std::vector<int> my_vect;

for( i = 0; i < n; i++ ) //runs n times
my_vect.push_back(i);//O(1)
total complexity : O(n)

(3) std::list<int> my_list;
for( i = 1; i < n; i++ ) //runs n-1 times
my_list.insert(i, 0);//inserts at position i//O(1)
total complexity :O(n)

(4) std::list<int> my_list;
for( i = 1; i < n; i++ ) //runs n-1 times
my_list.push_front(i);//O(n)//for moving in worst case
total complexity O(n*n)


Add a comment
Know the answer?
Add Answer to:
For each of the following two program fragments: a. Give an analysis of the running time...
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
  • 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;...

  • 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 =...

  • For the following program fragment give a Big-O analysis of the running time. Briefly explain your...

    For the following program fragment give a Big-O analysis of the running time. Briefly explain your answer: int t = 0; for(int i=1; i <= n; i++) for(int j=1; j <= i*i; j++)   if(j % i == 0)    t++;    What I have so far, O(1) + O(n) + O(n2) + O(1) + O(1) Drop Low order terms: O(n) + O(n2) And I believe the final answer to be O(n3), but not sure if just drop the O(n) or...

  • 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

  • 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…            } } }

  • 4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode...

    4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. howing your work is not required (although showing work may allow some partial t in the case your answer is wrong-don't spend a lot of time showing your work.). You MUST choose your answer from the following (not given in any particular order), each of which could be re-used (could be the answer for...

  • Compute the total running time for each of the functions in the following code. First, compute...

    Compute the total running time for each of the functions in the following code. First, compute the running time in terms of some constants a, b, c, d, e, etc. Show your work and give the answer in the text box. Then give the tightest big-O bound you can for each. (All of the functions are O(n!), but you can give a more informative bound for each.) void f(int n) {   int i = 1;   while (i <= sqrt(n)) {...

  • Give a big-Oh characterization, in terms of n,of the running time for each of the following...

    Give a big-Oh characterization, in terms of n,of the running time for each of the following code segments (use the drop-down): - public void func1(int n) { A. @(1). for (int i = n; i > 0; i--) { System.out.println(i); B. follogn). for (int j = 0; j <i; j++) System.out.println(j); c.e(n). System.out.println("Goodbye!"); D.@(nlogn). E.e(n). F.ein). public void func2 (int n) { for (int m=1; m <= n; m++) { system.out.println (m); i = n; while (i >0){ system.out.println(i); i...

  • In this lab we are going to complete a profile of two sorting algorithms by running...

    In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...

  • Exercises • Determine running time for the following code fragments: (a) a = b + c;...

    Exercises • Determine running time for the following code fragments: (a) a = b + c; d = a + e; (b) sum = 0; for (i=0; i<3; i++) for (j=0; j<n; j++) sum++; (c) sum=0; for (i=0; i<n<n; i++) sum++; (d) for (i=0; i < n-1; i++) for (j=i+1; j <n; j++) { tmp = A[i][j]; A[i][j] = A[j] [i]; A[j][i] = tmp; (e) sum = 0; for (i=1; i<=n; i++) for (j=1; j<=n; j+=2) sum++;

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