Question

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;

}

C.public static int method3(int n){

for (int i = n; i >= 0; i--){

for (int j = 0, j <= i + i; j++)

System.out.println(i + j);

}

return mid;

}

D. public static int method4(int [] a, int start, int end){

int ans = 0;

if (start >= end) ans = a[start];

else {

int mid = (start + end) / 2;

int x = method4(a, start, mid);

int y = method4(a, mid + 1, end);

print(a, start, end); //print each element in a from start to end

if (x < y) ans = x;

else ans = y;

}

return ans;

}

public static void print(int [] a, int s, int e){

for (int i = s; i <= e; i++) System.out.println(i);

}

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

A. answer)

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;

}

Here  for (int i = mid; i >= 0; i--) System.out.println(i); loop runs n/2 times.

and for (int i = mid + 1; i <= n; i++) System.out.println(i); loop runs n/2+1 times.

Total time is (n/2 ) + (n/2+1)=Big Theta(n)

B)answer)

public static int method2(int n){

for (int i = n; i >= 0; i / 3){

System.out.println(i );

}

return mid;

}

here for (int i = n; i >= 0; i / 3){ System.out.println(i ); runs log3 n times

Total time is Big Theta(log3 n)

c)answer)

public static int method3(int n){

for (int i = n; i >= 0; i--){

for (int j = 0, j <= i + i; j++)

System.out.println(i + j);

}

return mid;

}

for i=n , j loop runs 2n times

for i=n-1 ,j loop runs 2n-2 times..

for i=n-2 ,j loop runs 2(n-2) times.

'

'

for i=1 ,j loop runs 2 times.

total 2+2*2+2*3+------+2(n-1)+2n

=bigtheta(2(n^2))=bigtheta(n^2)

D.answer)

public static int method4(int [] a, int start, int end){---->assume This as T(n)

int ans = 0;

if (start >= end) ans = a[start];

else {

int mid = (start + end) / 2;

int x = method4(a, start, mid); --------------->Tn/2)

int y = method4(a, mid + 1, end);-------------->T(n/2)

print(a, start, end); //print each element in a from start to end

if (x < y) ans = x;

else ans = y;

}

return ans;

}

public static void print(int [] a, int s, int e){

for (int i = s; i <= e; i++) System.out.println(i);

}

here

public static void print(int [] a, int s, int e){

for (int i = s; i <= e; i++) System.out.println(i);

} runs bigtheta(e-s)time

T(n)=2T(n/2)+c

apply masters therom T(n)=bigtheta(nlogbb)=bigtheta(n)

total time is log(e-s)+n=bigtheta(n)

Add a comment
Know the answer?
Add Answer to:
What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...
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. t(n) is the runtime of following function, public static int f4(int [] a, int start,...

    1. t(n) is the runtime of following function, public static int f4(int [] a, int start, int end){ int ans = 0; if (start >= end) ans = a[start]; else { int mid = (start + end) / 2; int x = f4(a, start, mid); int y = f4(a, mid + 1, end); print(a, start, end); //print each element in a from start to end if (x < y) ans = x; else ans = y; } return ans; }...

  • PYTHON: Im stuck here, big O notation and runtime. What is it and Why are they...

    PYTHON: Im stuck here, big O notation and runtime. What is it and Why are they those? Please look at the pic, need help as Im confused. Thank You! def method3(n): for i in range(n): for j in range(100): for k in range(n): print(i+j+k) What is the runtime (tightest/closest bound in terms of O) for the above python function (method 3)? Please briefly explain. Enter your answer here def method4(n): for i in range(n): for j in range(n, o, -2):...

  • Analyze the runtime of c functions below and give a tight runtime bound for each. ....

    Analyze the runtime of c functions below and give a tight runtime bound for each. . . Both functions have the same best-case and worst-case runtime (so this is not an issue). Since we want a "tight" runtime bound, your final answer should be in big-m form. Show your work! "The runtime of foo() is e(< something >)" is not sufficient even if <something> happens to be correct. In other words, convince the reader of the correctness of your answer....

  • java Use the classes given in the previous question and write the output tha t is...

    java Use the classes given in the previous question and write the output tha t is generated when this program is run. You may write your answer as "Elaine 2 Jerry 1" or "Compiler Error" or "Runtime Error" If there is an error then explain why. public class Main{ public static void main(String args[]){ Object o = new Kramer(); ((George)(o)).method1(); ((Jerry)(o)). method1(); 0.method2(); public class George extends Elaine {! public void method1() { System.out.print("George 1 "); public class Jerry {...

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

  • Select all the valid asymptotic runtime bounds for the following function f2 in the worst case:...

    Select all the valid asymptotic runtime bounds for the following function f2 in the worst case: public static int f1 (int n) { int x = 0; for (int i = 0; i < n; i++) { x++; } return x; } public static int f2 (int n) { if (n <= 1) { return 1; } return f1(n) + f2(n/2) + f2(n/2); } Θ(n^2) O(n^2) Θ(log(n)) Θ(log^2(n)) Θ(nlog(n)) Ω(n) Ω(n^2)

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

  • 6. Using big-oh notation, give the runtime for each of the following recursive functions. You do...

    6. Using big-oh notation, give the runtime for each of the following recursive functions. You do not need to justify your answers: a) Int nonesense (int n) if (n <0) return 1; return nonsense (n-2) 1; b) int no nonesense (int n) if (n <0) return 1; return no_nonsense (n-1)+ no nonsense (n-1)

  • What are the valid runtime bounds for the method mystery with respect to argument n? Select...

    What are the valid runtime bounds for the method mystery with respect to argument n? Select all that apply. public static int mystery(int n) { for (int i = 1; i < n; i = i + i) { for (int j = 0; j < i; j++) { for (int k = 0; k < n; k++) { f0(n); } } } return -1; } public static void f0(int n) { int[] arr = new int[200]; for (int i...

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

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