Question

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 keep it.

Thanks

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

The time complexity of each line is O(1) + O(n) + + O(1) + O(1).

The third line(due to being nested in second line) runs total of which is approx. equal to .

Big-O complexity of a code segment is always equal to maximum of the time complexity of each line.

So,time complexity of this program fragment is max(O(1) , O(n) , , O(1) , O(1)) which is equal to .

This is the logic behind finding the Big- O time complexity of a program segment.

If the answer helped then please upvote.And for any queries,please comment.

Add a comment
Know the answer?
Add Answer to:
For the following program fragment give a Big-O analysis of the running time. Briefly explain your...
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
  • 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...

  • Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for...

    Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for i in range(n):             for j in range(n):                         test= test + i *j Q-2: Given3 the following code fragment what is its Big-O running time? for i in range(n):             test=test+1 for j in range(n):             test= test - 2 Q-3: Given the following code fragment what is its Big-O running time? i = n while i > 0:             k=2+2             i...

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

  • 1. Determine the appropriate big-o expression for each of the following functions, and put your answer...

    1. Determine the appropriate big-o expression for each of the following functions, and put your answer in the table we have provided in section 2-1 of ps5_parti. We've included the answer for the first function. (Note: We're using the “ symbol to represent exponentiation.) a (n) = 5n + 1 b. b(n) = 5 - 10n - n^2 o c(n) = 4n + 2log (n) d. e. d(n) = 6nlog (n) + n^2 e(n) = 2n^2 + 3n^3 - 7n...

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

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

  • What is the Big-Oh order of the following code fragment? The fragment is parametrized on the...

    What is the Big-Oh order of the following code fragment? The fragment is parametrized on the variable N. Assume that you are measuring the number of times j is decremented. public static void sort(Comparable[] a) { int N-a.length; for (int i = 1; i < N;i++) { for (int j = i; j > && less(a[5], a[j-1]); j--) //measure j -- exch(a, j, j-1); O(nlogn) O O(n^2) Q(n) Does not exist.

  • 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. Assume k is a constant given by the problem. for (i=1; i<=n; i++)      p = pow(i,k); // p = i to the power of k      for (j=1; j<=p; j++)           Some O(1) work      end for   end for

  • In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running...

    In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) int *a = new int [10]; // new is O(1) int size = 10; for (int i = 0; i < n; i ++) { if (i == size) { int newsize = 3*size/2; int *b = new int [newsize]; // new is O(1) for (int j = 0; j < size; j ++)...

  • . Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition. Estimate the big-O complexity for...

    . Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition. Estimate the big-O complexity for each of these algorithms, and justify your answer. To confirm your calculations, answers are provided at the end of the rubric. Your justification can be mathematical or written, formal or informal. Rubric: Correct Big-O classification of four problems Justification of four problems Big-O categories: 3.1. O(log n). 3.2. O(n). 3.3. O(n2). 3.4. O(1) Problem Code fragment 3.1 int sum = 0; int j =...

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