public class Search {
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;
}else if (arr[mid] > target) {
left = mid + 1;
}else {
right = mid - 1;
}
}
return false;
}
public static void main(String[] args) {
int[] array = {85, 63, 52, 34, 25, 17, 6};
int tar = 6;
boolean result = binarySearch(array, tar);
System.out.println("20052218 刘嘉源。");
if (result) {
System.out.println(tar + " 存在于数组中。");
} else {
System.out.println(tar + " 不存在于数组中。");
}
}
}