首页 > 解决方案 > 递归二进制搜索列表:“不是语句”?如何?

问题描述

处理一项任务,我一直遇到错误。我不太擅长Java,所以我不确定我做错了什么。作业要我评估递归二进制搜索,我在 binarySearch 方法中发现错误;它说“列表= {”1,2,5,7,9,15“};” 是表达式的非法开头,而不是语句,并且 ';' 预期的,即使显然有一个。我不知道……我有点紧张。

这是那个方法:

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

这是整个代码:

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

如果可以的话请帮忙。如果您发现任何其他错误,请告诉我。

标签: javarecursionbinarybinary-search

解决方案


问题

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

以上是java中初始化int数组的无效语句

使固定

使用作为输入传递的数组

笔记

初始化数组的正确方法是new int[]{1, 2, 5, 7, 9, 15};

一个可能的解决方案

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

调用

使用各自的参数从主方法调用二分查找方法

public static void main(String[] args) {
  binarySearch(numbers, 0, numbers.length - 1, 5); // to find 5
}

推荐阅读