使用Java语言,编写一个二分查找
时间: 2023-03-13 10:29:34 浏览: 93
我可以提供一些参考代码:
public class BinarySearch {
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (array[middle] == target) {
return middle;
} else if (array[middle] < target) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return -1;
}
}
阅读全文