繁体   English   中英

Java:子数组和二进制搜索算法

[英]Java: Subarrays and Binary search algorithm

我必须创建一个通过二进制搜索算法在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);
    }
}

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

但是,这不是您想要执行的操作。 用这种方法创建一个子数组是O(n) (记得它是一个副本..),而您不想这样做。

相反,请在方法中使用参数startend指示您的二进制搜索正在查找的范围。

还要注意,当找不到元素时增加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)递归。

希望可以阐明如何执行二进制搜索。
祝好运!

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

因此,在递归调用中,您只需传递一个数组长度一半的数组。 您可以仅使用数组的前半部分或后半部分创建一个新数组: 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设置为您的开始变量。 如果搜索的Num小于您要查找的数字,则将min设置为开始。 在方法开始时,如果当前起始位置是<= max或> = min,则返回-1,因为您将搜索之前已经查找过的位置,因此要查找的数字不在数组中。

尝试这个:

  • end添加为@anonymous建议的方法调用。
  • ArraySearch每个递归调用中,将startend的值设置为
    arr1[start] < searchNum < arr1[end]
  • 并且在每个递归调用中,使startend之间的距离(大约)为上一个调用的一半。

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

因为在线性搜索中,您可能不会获得-1的返回值,但是如果使用通过二进制搜索来查找int值的方法 ,则可以将返回值设为-1。

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

Arrays.copyOfRange(Object[] src, int from, int to)

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

我认为您正在寻找的东西如下所述:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM