Question

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 func2(int n) { for (int m=1; m <= n ; m++) { system.out.println (m); i = n; while (i > 0) { system.out.println(

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

public void func1(int n) { for (int i = n; i > 0; i--) { System.out.println(i); for (int j = 0; j <i; j++) System.out.println

outer for loop is running for i = n to i = 1 (>0) which means total n times. inner for loop is running for j = 0 to j = i-1 (<i) which means i times so,

  • when i = n in outer loop, inner loop will run n times
  • when i = n-1 in outer loop, inner loop will run n-1 times
  • .....................
  • when i = 1 in outer loop, inner loop will run 1 time

Total = n + n-1 + n-2 + ..... + 2 + 1 = n(n+1)/2 = \small \frac{n^2}{2} + \frac{n}{2} =  \bold{\theta(n^2)}

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

for loop is running for m = 1 to m = n which means total n times. while loop inside for loop is running for fixed logn times (every time value of i is divided by 2) .

Total = n*(logn) = nlogn =  \bold{\theta(nlogn)}

public void func2(int size) { if (size%2 == 0) System.out.println(Even elements); else System.out.println(Odd elements);

There are no loops. If-else condition will take constant time as there is only one print statement which will get executed.

Total = C (constant) =  \bold{\theta(1)}

public void func1(int n, String msg) { int i=0, j=0, k=0; for (i = n; i > 0; i--) for(j = 0; j<i; j++) for (k = 0; k < 1000;

outer-most for loop is running for i = n to i = 1 (>0) which means total n times. outer for loop is running for j = 0 to j = i-1 (<i) which means i times. Inner for loop will run 1000 time for each value of j which is constant. so,

  • when i = n in outer-most loop, outer loop will run n times => Inner loop will run 1000*n times
  • when i = n-1 in outer-most loop, outer loop will run n-1 times => Inner loop will run 1000*n-1 times
  • .....................
  • when i = 1 in outer-most loop, outer loop will run 1 time => Inner loop will run 1000 times

Total = n*1000 + (n-1)*1000 + (n-2)*1000 + ..... + 2*1000 + 1*1000 = 1000*(n(n+1)/2) = \small \frac{500n^2}{2} + \frac{500n}{2} =  \bold{\theta(n^2)}

Note that higher order polynomial is considered as complexity.

Add a comment
Know the answer?
Add Answer to:
Give a big-Oh characterization, in terms of n,of the running time for each of the following...
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
  • 3. Determine the general result (in terms of n) of the following recursive function (4 pts.)...

    3. Determine the general result (in terms of n) of the following recursive function (4 pts.)               public static void func2(int n)      {         if(n == 1)            System.out.println(“*”);         else         {            for (int i = 1; i <= n; i++)               System.out.print(“*”);            System.out.println();            func2(n-1);                      }               } 4. Does func2 above perform down or bottom up computation? ___________________ (2 pts.)

  • Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms...

    Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. Show your work b) void func(int n) { for (int i = 0; i < n; i = i + 10) { for (int j = 0; j < i; ++i) { System.out.println("i = " + i); System.out.println("j = " + j);

  • Provide a "big oh" run-time analysis for each of the following. When a value of “n”...

    Provide a "big oh" run-time analysis for each of the following. When a value of “n” is used, it is the size of the input. 4.) void problem 40 cin n min max for (int i min i n, i++) for (int j- 1: j< max, j++) tota while (total n tota total 2 total 5.) void problem 50 cin n; for (int i 0: i n, i++) for (int j 0; j i2; j++) for (int k 0; k...

  • Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of...

    Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku {               public String[][] makeSudoku(String s) {              int SIZE = 9;              int k = 0;              String[][] x = new String[SIZE][SIZE];              for (int i = 0; i < SIZE; i++) {                     for (int j = 0; j < SIZE; j++)...

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

  • What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...

    What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ)....

    1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...

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

  • Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...

    Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation. My code is! public class MessageQueue{ public MessageQueue(int capacity){ elements = new Message[capacity];...

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
ADVERTISEMENT