public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid; // 找到了目标元素,返回数组索引
} else if (arr[mid] < target) {
low = mid + 1; // 目标元素在右半部分,调整左指针
} else {
high = mid - 1; // 目标元素在左半部分,调整右指针
}
}
return -1; // 没有找到目标元素,返回 -1
}
}