public class BinarySearch {
public static boolean 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 true;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
public static void main(String[] args) {
int[] myArray = {6, 17, 25, 34, 52, 63, 85};
int targetElement = 6;
boolean result = binarySearch(myArray, targetElement);
if (result) {
System.out.println("元素 " + targetElement + " 存在于数组中。");
} else {
System.out.println("元素 " + targetElement + " 不存在于数组中。");
}
}
}