public class BinarySearch {
static boolean binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return true;
}
if (arr[mid] > target) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return false;
}
public static void main(String[] args) {
int[] sortedArray = {6, 17, 25, 34, 52, 63, 85};
int targetElement = 6;
boolean result = binarySearch(sortedArray, targetElement);
if (result) {
System.out.println("元素 " + targetElement + " 存在于数组中。");
} else {
System.out.println("元素 " + targetElement + " 不存在于数组中。");
}
}
}