【问题标题】:Recursive binary search list: "not a statement"? how?递归二进制搜索列表:“不是语句”?如何?
【发布时间】:2021-02-22 14:40:12
【问题描述】:

正在处理一项任务,但我一直遇到错误。我不太擅长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);
                }
            }
        }    
    }
}

如果可以,请提供帮助。如果您发现任何其他错误,请告诉我。

【问题讨论】:

  • 不幸的是,您的问题表明您在这里遗漏了一些非常基本的概念,这些概念在本论坛的问答形式中很难解释。我建议您阅读标准 Java 教程(例如 docs.oracle.com/javase/tutorial),如果您有具体问题,请返回此处。
  • @zaibeh,正如 AKSSingh 所指出的,您的初始化不正确。另一方面,您不应该在二分搜索函数中初始化。您应该重用传递给函数的输入数组和目标值
  • 正确方法:list = new int[]{1, 2, 5, 7, 9, 15}; 但是由于代码中的逻辑缺陷,这样做会导致进一步的错误。检查@Horse 答案以了解正确的逻辑。 请访问此链接以获取有关如何在 Java 中声明和初始化数组的更多信息。请阅读第一个答案中的前两个 cmets,因为它们包含与您的问题类似的信息。 stackoverflow.com/questions/1200621/…

标签: java recursion binary binary-search


【解决方案1】:

问题

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
}

【讨论】:

  • 您可以添加list = new int[]{1, 2, 5, 7, 9, 15}; 作为另一种替代解决方案。
  • @AKSingh,感谢您的建议。实际上,在原始代码中,OP 不应重新初始化数组,而应按原样重用输入数组。更新答案:)
  • 是的,你是对的。递归失败。我会更新我的评论。感谢您指出这一点。
猜你喜欢
  • 1970-01-01
  • 2013-08-23
  • 2016-11-14
  • 2021-03-30
  • 2021-04-28
  • 2020-09-25
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
相关资源
最近更新 更多