0

Working on an assignment and I keep running into an error. I'm not really that good at Java so I'm not sure what I'm doing wrong. The assignment wants me to evaluate recursive binary search, and I'm finding an error in the binarySearch method; it says "list = {"1,2,5,7,9,15"};" is an illegal start to expression, not a statement, and ';' expected even though there clearly is one. I don't know...I'm getting a little stressed out.

Here's that method:

//recursive binary search
    public boolean binarySearch(int[] list, int target, int low, int high) {
        list ={"1,2,5,7,9,15"}; 
        target = 5;
        boolean result = binarySearch(list, target, 0, list.length-1);

        executions++;
        int mid = (low + high) / 2;

        if(!result){
            System.out.println("Target not found -- bad search.");
        }else{
            System.out.println("Target found -- sucessful search!");
        }    
        if (list[mid] == target) {
            return true;
        } else {
            if (low > high) {
                return false;
            } else {
                comparisons++;
                if (list[mid] < target) {
                    return binarySearch(list, target, mid + 1, high);
                } else {
                    return binarySearch(list, target, low, mid - 1);
                }
            }

And here's the entire code:

public class Problem2 {
    public static int executions = 0;
    public static int comparisons = 0;
    public static int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

    public static void main(String[] args) {
        System.out.println("2nd fibonacci number: " + fibonacci(2));
        System.out.println("Number of executions: " + executions);
        System.out.println("Number of comparisons: " + comparisons);
        executions = 0;
        comparisons = 0;
        System.out.println();

        System.out.println("Number of operations: " + executions);
        System.out.println("Number of comparisons: " + comparisons);
        executions = 0;
        comparisons = 0;
        System.out.println();

        System.out.println("Factorial of 7: " + nFactorial(7));
        System.out.println("Number of operations: " + executions);
        System.out.println("Number of comparisons: " + comparisons);
        executions = 0;
        comparisons = 0;
    }
    //recursive Nfactorial
    public static int nFactorial(int n) {
        comparisons++;
        if (n <= 1) {
            return 1;
        } else {
            executions++;
            return n * nFactorial(n - 1);
        }
    }

    //recursive fibonacci number
    public static int fibonacci(int n) {
        comparisons++;
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else {
            executions++;
            return fibonacci(n - 1) + fibonacci (n - 2);
        }
    }
    //recursive binary search
    public boolean binarySearch(int[] list, int target, int low, int high) {
        list ={"1,2,5,7,9,15"}; 
        target = 5;
        boolean result = binarySearch(list, target, 0, list.length-1);

        executions++;
        int mid = (low + high) / 2;

        if(!result){
            System.out.println("Target not found -- bad search.");
        }else{
            System.out.println("Target found -- sucessful search!");
        }    
        if (list[mid] == target) {
            return true;
        } else {
            if (low > high) {
                return false;
            } else {
                comparisons++;
                if (list[mid] < target) {
                    return binarySearch(list, target, mid + 1, high);
                } else {
                    return binarySearch(list, target, low, mid - 1);
                }
            }
        }    
    }
}

Please help if you can. If you see anything else that's wrong, please let me know.

Zaibeh
  • 1
  • Unfortunately your question demonstrates that you are missing some pretty basic concepts here which are going to be difficult to explain in the Q&A format of this forum. I suggest you work through a standard Java tutorial (such a https://docs.oracle.com/javase/tutorial/ ) and then come back here if you have a specific question. – sprinter Feb 22 '21 at 06:24
  • @zaibeh, As pointed by AKSSingh, your initialization is incorrect. On a different note, you should not initialize within binary search function. You should reuse the input array and target values passed to the function – Horse Feb 22 '21 at 06:34
  • The correct way: `list = new int[]{1, 2, 5, 7, 9, 15};` *However doing so will result in further error due to logical flaw in your code*. Check @Horse answer for correct logic. *Do visit this link for more information on how to declare and initialize arrays in Java. Do read the first two comments in the first answer as they contain information similar to your question.* https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java – AKSingh Feb 22 '21 at 06:39

1 Answers1

1

Issue

list ={"1,2,5,7,9,15"};

The above is an invalid statement in java to initialize a int array

Fix

Use the array passed as input

Note

The correct way to initialize array is new int[]{1, 2, 5, 7, 9, 15};

A possible solution

boolean binarySearch(int[] arr, final int left, final int right, final int target) {
        if (left > right) {
            return false;
        }
        final int mid = left + (right - left) / 2;
        if (arr[mid] == target) { // if matched, return true
            return true;
        } else if (arr[mid] < target) { // if candidate is less, then more towards right. drag left towards right(i.e: mid + 1)
            return binarySearch(arr, mid + 1, right, target);
        } else { // else drag right towards left (i.e. mid - 1)
            return binarySearch(arr, left, mid - 1, target);
        }
    }

Invocation

Call the binary search method from the main method with respective parameters

public static void main(String[] args) {
  binarySearch(numbers, 0, numbers.length - 1, 5); // to find 5
}
Horse
  • 2,295
  • 1
  • 2
  • 10
  • You may add `list = new int[]{1, 2, 5, 7, 9, 15};` as another alternative solution. – AKSingh Feb 22 '21 at 06:31
  • @AKSingh, Thank you for the suggestion. Actually in the original code, OP should not reinitialize the array and should reuse the input array as it is. Updated answer :) – Horse Feb 22 '21 at 06:33
  • Yes you are right. The recursion fails. I will update my comment. Thank you for pointing this out. – AKSingh Feb 22 '21 at 06:37