Question

Show the analytical time complexity. Show all work. public static void foo(int n, char A, char...

Show the analytical time complexity. Show all work.

public static void foo(int n, char A, char B, char C) {

      if (n<=0) return;   // primitive operation

      foo(n-2, A, C, B);

      for (int i=0; i<n; i++) System.out.println(“n=” + n); // primitive operation

      foo(n-2, B, A, C);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
there are two recursive calls of size n-2
and work done in each recursive call is O(n)

recurrence relation:
---------------------
T(n) = 2T(n-2) + O(n)

solving the recurrence relation:
---------------------------------
T(n)
= 2T(n-2) + O(n)
= 2(2T(n-4) + O(n-2)) + O(n)
= 2(2T(n-4) + O(n)) + O(n)  (because O(n-2) is O(n))
= 2(2(2T(n-6) + O(n)) + O(n)) + O(n)
= 2^3 T(n-6) + 2^2(O(n)) + 2^1(O(n)) + 2^0(O(n))
= (2^(n/2) + ... + 2^2 + 2^1 + 2^0) * O(n)
= (2^(n/2) (1 + 1/2 + 1/4 + ...)) * O(n)
<= 2^(n/2) * 2 * O(n)

so, T(n) is O(n * 2n/2)
Add a comment
Know the answer?
Add Answer to:
Show the analytical time complexity. Show all work. public static void foo(int n, char A, char...
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) Show the calculation complexity of ciz function as O. static void ciz(int m) {    ...

    1) Show the calculation complexity of ciz function as O. static void ciz(int m) {     for(int i=1; i<=m; i++)       System.out.print("X"); } 2) By using the answer to 1, Show the calculation complexity of main function as O. import java.util.Scanner; ... public static void main(String[] arg) {     int n;     Scanner oku = new Scanner(System.in);     System.out.print("N=");     n=oku.nextInt();     for(int m=1; m<=n; m++) {       ciz(m);       System.out.println();     } }

  • Consider the following Java classes: class A{ public int foo () { return 1; } public...

    Consider the following Java classes: class A{ public int foo () { return 1; } public void message () { System.out.println( "A" + foo()); } } class B extends A { public int foo() {return 2; } } class C extends B { public void message () { System.out.println( "C" + foo()); } } (i) What are the outputs of the following code? (ii) What would be the outputs if Java used static dispatching rather than dynamic dispatching? B b...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • 1) In the Quiz class, the foo method has the following API: public void foo( int...

    1) In the Quiz class, the foo method has the following API: public void foo( int x, String s) Which method call(s) would be correct assuming both a and y are integer values and assuming each statement is complete? a. Quiz q = new Quiz(); a = q.foo( y, “Maybe?” ); b. Quiz q = new Quiz(); q.foo( 1, “Hmmm!” ); c. Quiz q = new Quiz(); Quiz.foo( y, “You think” ); d. Both b and c 2) In the...

  • In the Quiz class, the foo method has the following API : public static double foo(...

    In the Quiz class, the foo method has the following API : public static double foo( int i, string s, char c) how many arguments does foo take? is it 3 right? What is the output of this code sequence? int a = Math.min( 5, 8 ); System.out.println( a ); What is the output of this code sequence? System.out.print( Math.round( 3.5 ) ); What is the output of this code sequence? double d = Math.pow( 2, 3 ); System.out.println( d...

  • public class F2{ public static int foo(ArrayList<Integer> a) { int sum = 0; for(int i =...

    public class F2{ public static int foo(ArrayList<Integer> a) { int sum = 0; for(int i = 0; i <a.size(); i++){ if(i % 3 == 1) sum += a.get(i); else if(i % 3 == 2) sum -= a.get(i); else sum++; } return sum; } } What do the following method calls return? 1. foo(new ArrayList<>(Arrays.asList(1,2,3,4))) 2. foo(new ArrayList<>()) 3. foo(new ArrayList<>(Arrays.asList(1,2,-3,4325,-2))) 4. foo(new ArrayList<>(Arrays.asList(0,0,0)))

  • How can i print a diamond implementing these methods public static void printNChars(int n, char c))....

    How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...

  • Show the output of the following piece of code: public static void main(String[] args) { char...

    Show the output of the following piece of code: public static void main(String[] args) { char x = 'a'; char y = 'c'; System.out.println(++x); System.out.println(y++); System.out.println(x - y); }

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

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