【问题标题】:recursion method in the binary search二分查找中的递归方法
【发布时间】:2015-12-11 23:00:00
【问题描述】:

我正在尝试实现一个二进制算法,但我真的不知道如何使用递归方法编写这个程序。有人可以帮我写这个方法吗? 我已经为我写了最简单的方法:

import Prog1Tools.IOTools;
public class BinarySearch {

     public static void showArray(int[] array) {
         for(int x : array) System.out.print (x + " ");
         System.out.println ();
         }

     public static void fillArray(int[] array, int arrayFirst) {
         int i = 0;     
         while (i < array.length){
             array[i] = arrayFirst;
             i++;
             arrayFirst++;
         }   
    }

     public static void main(String[] args) {

         int l, p, s;
         int arrayEnd = IOTools.readInt("Type a last number in the array : ");
         int arrayFirst = IOTools.readInt("Type a first number in the array : ");         
         int[] nums = new int[arrayEnd+1-arrayFirst ];  
         fillArray(nums, arrayFirst);
         showArray(nums);    
         System.out.println ("Could you please choose a number from the array above? " );  
         l = 0;
         p = arrayEnd-arrayFirst;
         loop: while (l <= p) {
              s = (l + p) / 2;
              String question = IOTools.readString("Is your number "+nums[s] + " or higher ?[You can answer: yes or higher] ");

                switch (question){
                case "yes":
                    System.out.println("I found a number "+nums[s]+" Your number has an index "+s +" in the array");
                    break loop;
                case "higher":
                    l = s + 1;  
                    break; 
                }               
              }
          }  
     }

我试过这样的方法,但它不起作用

    public static int recursiveBinarySearch(int[] sortedArray, int start, int end, String question) {

        if (start < end) {
            int mid = start + (end - start) / 2; 
            if (question=="higher") {
                return recursiveBinarySearch(sortedArray, start, mid, question);

            } else if (question=="lower") {
                return recursiveBinarySearch(sortedArray, mid+1, end , question);

            } else {
                return mid;  
            }
        }
        return -(start + 1); 
    }

【问题讨论】:

  • 可能和这个问题一样:stackoverflow.com/questions/19012677/…
  • 您应该使用.equals 比较字符串,而不是==。见this question
  • 我认为您的问题可能更多是关于需要了解递归?
  • 你能举个例子解释一下吗? :)
  • 看看你使用if (question.equals("higher"))if (question.equals("lower"))是否有效。而且您还需要更新问题变量。

标签: java recursion


【解决方案1】:
public boolean binaryS(int A[],int left, int right, int x){
    int middle;
    //check if there is any array left to search
    if (left > right) return false;
    //determine the middle of this array section
    middle = (left + right) / 2;
    //is the middle what we are looking for?
    if (A[middle] == x) return true;
    //search the half of the array that might contain x
    if (A[middle] > x) { //search for x to the left
        return binaryS(A, left, middle - 1, x);
    } else { //search for x to the right
        return binaryS(A, middle + 1, right, x);
    }
}

【讨论】:

    【解决方案2】:

    Bob 的答案是一个不错的二分搜索。

    如果您想获得用户输入(并保留您的结构):

    public static int recursiveBinarySearch(int[] sortedArray, int start, int end) {
        if (start < end) {
            int mid = (start + end) / 2;
            String question = IOTools.readString("Is your number compared to "+sortedArray[mid] + " lower or higher?[You can answer: equal, lower or higher] "); 
            if (question.equals("higher")) {
                return recursiveBinarySearch(sortedArray, mid+1, end, question);
            } else if (question.equals("lower")) {
                return recursiveBinarySearch(sortedArray, start, mid-1, question);
            } else {
                return mid;  
            }
        }
        System.out.println("Something went wrong...");
        return -1;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 2012-02-16
      • 2019-02-08
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 2020-11-13
      • 2021-04-08
      • 1970-01-01
      相关资源
      最近更新 更多