Java:子数组和二进制搜索算法
我必须创建一个程序,通过二进制搜索算法在arr1中搜索int,如果找不到int,则返回-1。我在创建子阵列时遇到问题。它在没有它们的情况下可以工作,但我只有一个解决办法,可以解决int大于start的问题Java:子数组和二进制搜索算法,java,algorithm,binary-search,arrays,Java,Algorithm,Binary Search,Arrays,我必须创建一个程序,通过二进制搜索算法在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 = arr
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);
}
}
首先回答您的问题-您可以使用..创建子数组 然而,这不是你想要做的。以这种方式创建子阵列是不可能的,因为它是一个副本,而您不想这样做 相反,在方法中使用参数start和end,它们指示二进制搜索现在要查找的范围 还请注意,当未找到元素时,增加1不是二进制搜索,相反,您需要设计算法,使其始终“跳转”剩余数组的一半,而不是固定大小 根据这一点,您的方法签名将类似于:
public static int arraySearch( int [] arr1, int start, int end, int searchNumber){ ... }
然后您将设置一个mid元素:int mid=start+end-start/2;
您将检查所需的数字是否大于arr1[mid]。
如果是,您将使用ArraySearchRR1、mid+1、end、searchNumber递归,如果较小,则使用ArraySearchRR1、start、mid、searchNumber递归
希望这能澄清如何实现二进制搜索。
祝你好运 因此,在递归调用中,只需传递数组长度的一半。您可以仅使用阵列的前半部分或后半部分创建新阵列: 另一个选项是使用最大和最小整数来标记正在查看的数组的哪个部分。如果searchNum小于正在查看的值,请将max设置为start变量。如果search Num小于正在查看的数字,请将min设置为start。 在方法开始时,如果当前开始为=min return-1,因为您将搜索之前已查找过的位置,因此您要查找的数字不在数组中。请尝试以下操作: 按照@anonymous的建议为方法调用添加end。 在ArraySearch的每次递归调用中,使start和end的值 arr1[start]
Arrays.binarySearch(arr1, start, arr1.length, searchNum);
然而,由于任务需要重新设计轮子,你不能仅仅使用它。但是有很多提示可以帮助您设计程序
您在这里面临的主要障碍之一是您正在使用的方法签名:
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){
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);
}
我想你要找的是下面提到的东西:
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);
}
}
}
好的,根据你的问题,你想创建一个程序,通过二进制搜索来搜索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);
我不明白你的问题。你说我在创建子数组时遇到麻烦是什么意思?这不是二进制搜索。当目标位于阵列的右半部分时,只能将起始值增加1。您需要以递归方法开始和结束。查看二进制搜索,假设给定数组已排序。如果数组未排序,二进制搜索将无法工作。先分类
Arrays.copyOfRange(Object[] src, int from, int to)
System.arraycopy(Object[] src, int srcStartIndex, Object[] dest, int dstStartIndex, int lengthOfCopiedIndices);