在java上给出错误索引的数组的二进制搜索
Posted
技术标签:
【中文标题】在java上给出错误索引的数组的二进制搜索【英文标题】:binary search on array giving wrong index on java 【发布时间】:2022-01-14 21:11:13 【问题描述】:我正在编写一个不必导入java数组的代码,它可以插入、删除和搜索一系列值;但是当调用binSearch()
方法时,输出会显示它没有在列表中找到值。在调用 binSearch()
之前,我已经在 main 方法中对数组进行了排序,但它仍然没有找到值的索引。
我还是 java
的初学者,所以如果你能以初学者的水平向我解释,那就太好了。
private static int[] insertElement(int index, int array[], int list[], int i)
int length = array.length;
int destination[] = new int[length + 1];
for (int j = 0; j < destination.length; j++)
System.arraycopy(array, 0, destination, 0, index);
System.arraycopy(array, index, destination, index + 1, length - index);
destination[index] = list[i];
System.out.println(list[i] + " is inserted in the list.");
return destination;
private static int[] deleteElement(int index, int[] array, int list[], int i)
boolean[] deleteNumber = new boolean[array.length];
int size = 0;
for (int j = 0; j < array.length; j++)
if (array[j] == list[i])
deleteNumber[j] = true;
System.out.println(list[i] + " is removed from the list.");
else
deleteNumber[j] = false;
size++;
int[] newArr = new int[size];
int in = 0;
for (int j = 0; j < array.length; j++)
if (!deleteNumber[j])
newArr[in++] = array[j];
return newArr;
public static int binSearch(int[] array, int search[], int i)
int left = 0;
int right = array.length - 1;
if (left <= right)
int middle = (left + right) / 2;
if (search[i] < array[middle])
right = middle - 1;
else if (search[i] > array[middle])
left = middle + 1;
else
System.out.print(search[i] + " is found at location ");
return middle;
return -1;
public static void main(String args[])
int arr[] = 2, 4, 6, 8, 9, 10, 20, 33, 44, 45, 68, 88;
int index = 1;
//insert element
int newIndex = index - 1;
int s[] = 3, 78, 98, 12;
for (int i = 0; i < s.length; i++)
arr = insertElement(newIndex, arr, s, i);
//delete element
int d[] = 20, 44, 89;
for (int i = 0; i < d.length; i++)
arr = deleteElement(newIndex, arr, d, i);
//sort the array
for (int i = 0; i < arr.length; i++)
for (int j = i + 1; j < arr.length; j++)
if (arr[i] > arr[j])
int swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
//search for element in array
BinarySearch ob = new BinarySearch();
int a[] = 8, 45, 88, 90;
for (int i = 0; i < a.length; i++)
int result = ob.binSearch(arr, a, i);
if (result == -1)
System.out.println(a[i] + " is not found in the list.");
else
System.out.println(result + ".");
//print array
printArray(arr);
【问题讨论】:
【参考方案1】:只需将 if 子句替换为 while,因为您必须在二叉搜索树的所有级别进行搜索。 改变
if (left <= right)
到
while (left <= right)
而且您的 insertElement 方法也存在小问题。
for (int j = 0; j < destination.length; j++)
System.arraycopy(array, 0, destination, 0, index);
System.arraycopy(array, index, destination, index + 1, length - index);
insertElement 方法中不需要再次进行 for 循环,因为您已经在循环遍历需要在 main 方法中添加的数组。
【讨论】:
【参考方案2】:我在我的代码中发现了错误。 binSearch() 方法应该使用 while 循环,而不是 if 循环。整个代码现在完美运行。
public static int binSearch(int[] array, int search[], int i)
int left = 0;
int right = array.length - 1;
while (left <= right)
int middle = (left + right) / 2;
if (search[i] < array[middle])
right = middle - 1;
else if (search[i] > array[middle])
left = middle + 1;
else
System.out.print(search[i] + " is found at location ");
return middle;
return -1;
【讨论】:
以上是关于在java上给出错误索引的数组的二进制搜索的主要内容,如果未能解决你的问题,请参考以下文章