public class BinarySearch {
public static int binarySearch(int arr[], int left, int right, int x) {
while (left <= right) {
int mid = (left + right)/ 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int arr[] = {85, 63, 52, 34, 25, 17, 6};
int n = arr.length;
int x = 6;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) {
System.out.println("元素 " + x + " 不在数组中");
} else {
System.out.println("元素 " + x + " 在数组中的索引为 " + result);
}
}
}