Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class DriverCode{ public static void main(String[] args){ mystery(100); }

ID: 3860324 • Letter: P

Question

public class DriverCode{
public static void main(String[] args){
   mystery(100);
   }
   public static void mystery(int x){
       if(x<5){
         return;
       }
      else{
         System.out.println(x);
          if(x%3==2){
             mystery((int)(x/3));
          }
          else{
             mystery((int)(x/2));
             }
          }
       }
   }

1. Including the original call to the mystery method, how many times is the method called?

2. What is the correct technical term that describes the code provided in the first IF statement in the mystery method?

3. Consider the following sort methods and the following two methods.

Give the “Big-Oh” time complexity for each method.

a. Merge sort time complexity:

b. Sequential search time complexity:

c. Selection sort time complexity:

d. Binary search time complexity:

4. Place the methods described above in order of their “Big-Oh” time complexity, from slowest to fastest growth(i.e fromlowest to highest complexity). As a reminder, here are the methods again:

-Merge sort time complexity:

-Sequential search time complexity:

-Selection sort time complexity:

-Binary search time complexity:

Explanation / Answer

1) 5 times

intitial call mysetry(100) as 100 is > 5 and 100%3 !=2 calls mystery(100/2)

mysetry(50)   as 50 >5 and 50%3==2 calls mystery(50/3)

mysetry(16) as x>5 and x%3 !=2 calls mystery(16/2)

mysetry(8) as x>5 and x%3 ==2 calls mystery(8/3)

mysetry(2)

2) It is called return statement

3)

a. Merge sort time complexity: O(nlogn)

b. Sequential search time complexity: O(n) as it is linear search

c. Selection sort time complexity: O(n^2)

d. Binary search time complexity: O(logn)

4) Binary search < Sequential search < Merge sort < Selection sort