【问题标题】:Java: Subarrays and Binary search algorithmJava:子数组和二分搜索算法
【发布时间】:2014-05-25 19:24:10
【问题描述】:

我必须创建一个程序,通过二进制搜索算法在 arr1 中搜索 int,如果未找到 int,则返回 -1。我在创建子数组时遇到了麻烦。目前没有它们它可以工作,但我只有一个解决方法,只要 int 大于 start。

public class Question8p2 {
    public static void main(String[] args){
        int[] arr1 = {2,45,234,567,876,900,976,999};
        int start = arr1.length / 2;
        int searchNum = 900;
        arraySearch(start, searchNum, arr1);
        System.out.println(arraySearch(start, searchNum, arr1));
    }

    public static int arraySearch(int start, int searchNum, int [] arr1){
        if (arr1[start] == searchNum){
            return start;
        }

        if (arr1[start] < searchNum){
            start = start + 1;
        }

        if (arr1[start] > searchNum){
            start = start / 2;
        }

        if (start == arr1[0] || start == arr1.length){
            return -1;
        }

        return arraySearch(start, searchNum, arr1);
    }
}

【问题讨论】:

标签: java algorithm binary-search arrays


【解决方案1】:

首先回答您的问题 - 您可以使用 Arrays.copyOfRange(array,from,to).. 来创建子数组。

但是,这不是您想要做的。以这种方式创建子数组是O(n)(记住它是一个副本..),你不想这样做。

相反,在您的方法中使用参数start 和end - 表示您现在正在寻找的二进制搜索范围。

另请注意,当未找到元素时增加 1 不是二分搜索,相反,您需要设计算法,使其始终“跳跃”剩余数组的一半 - 而不是恒定大小。

据此,您的方法签名将类似于:

public static int arraySearch( int [] arr1, int start, int end, int searchNumber){ ... }

然后您将设置一个mid 元素:int mid = start + (end - start)/2;
您将检查所需的数字是否大于arr1[mid]。
如果是,您将使用arraySearch(arr1,mid+1,end,searchNumber) 进行递归,如果它更小,您将使用arraySearch(arr1,start,mid,searchNumber) 进行递归

希望能阐明您的二分搜索应该如何实现。
祝你好运!

【讨论】:

    【解决方案2】:

    在 Java 中执行该搜索的最佳方法是:

    Arrays.binarySearch(arr1, start, arr1.length, searchNum);
    

    但是,由于作业需要重新发明轮子,因此您不能只使用它。但是您可以从Arrays.binarySearch 获得一些提示来帮助您设计程序。

    您在这里面临的主要障碍之一是您正在使用的方法签名:

    public static int arraySearch(int start, int searchNum, int [] arr1)
    

    如果您将方法签名与Arrays.binarySearch 方法使用的签名不同,则任务会变得容易得多。所以你应该实现一些允许指定结束索引的东西。我还会更改参数顺序以匹配Arrays.binarySearch,甚至称其为binarySearch:

    public static int binarySearch(int [] a, int fromIdx, int toIdx, int key)
    

    所以你应该这样做。

    public static int binarySearch(int [] a, int fromIdx, int toIdx, int key){
        if(fromIdx == toIdx) return -1;
    
        int pivotIdx = (fromIdx + toIdx)/2;
    
        if(a[pivotIdx] < key) return binarySearch(a, fromIdx, pivotIdx, key);
        if(a[pivotIdx] == key) return pivotIdx;
        if(a[pivotIdx] > key) return binarySearch(a, pivotIdx, toIdx, key);
    }
    

    【讨论】:

      【解决方案3】:

      因此,在您的递归调用中,您只需要传递一个数组长度的一半。您可以仅使用数组的前半部分或后半部分创建一个新数组: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29

      另一个选项是使用最大和最小整数来标记您正在查看的数组的哪个部分。如果您的 searchNum 小于您正在查看的值,请将 max 设置为您的 start 变量。如果搜索 Num 小于您正在查看的数字,请设置 min 开始。 在您的方法开始时,如果您当前的开始是 = min 返回 -1,因为您将搜索之前已经查看过的位置,因此您要查找的数字不在数组中。

      【讨论】:

        【解决方案4】:

        试试这个:

        • 按照@anonymous 的建议将end 添加到您的方法调用中。
        • 在每次递归调用ArraySearch 时,使start 和end 的值满足
          arr1[start] &lt; searchNum &lt; arr1[end]
        • 并且在每次递归调用中,使start 和end 之间的距离为上一次调用的一半(大致)。

        【讨论】:

          【解决方案5】:

          好的,根据您的问题,您想创建一个程序,通过二进制搜索在 arr1 中搜索 int - 好吧,为此您可以使用线性搜索以及二进制搜索。

          因为在linear search 中,您可能不会得到-1 的返回值,但如果您是using method,则可以通过二进制搜索找到int 值。

          而创建子数组的解决方案如下

          Arrays.copyOfRange(Object[] src, int from, int to)
          
          System.arraycopy(Object[] src, int srcStartIndex, Object[] dest, int dstStartIndex, int lengthOfCopiedIndices);
          

          【讨论】:

            【解决方案6】:

            我认为您正在寻找的内容如下所述:

                private int BinarySearch(int[] array, int low, int high, int number)
                {
            
                    // First get the middle index using high and low index 
                    int medium = (high + low) / 2;
            
                    // Check if the number you are looking is same as the one at middle index?
                    // If that is the case, just return the middle index
                    if (array[medium] == number)
                    {
                       return medium;
                    }
                    else
                    {
                        // if the number is not found in previous condition resume the operation. 
            
                        // Check if number is greater than the middle index number 
                        if (array[medium] < number)
                        {
                           // call the search operation by replacing your low index with middle + 1
                           return  BinarySearch(array, medium + 1, high, number);
                        }
                        else
                        {
                           // Here number is less than the middle index number 
                           // call the search operation by replacing your high index with middle-1 
                           return  BinarySearch(array, low, medium - 1, number);
                        }
                    }
                }
            

            【讨论】:

              猜你喜欢
              • 2020-11-21
              • 1970-01-01
              • 1970-01-01
              • 2021-03-23
              • 2017-04-16
              • 2015-07-12
              • 2017-07-02
              • 2013-04-23
              相关资源
              最近更新 更多