package nomal;
import java.util.Arrays;
public class BinarySearch {
public static boolean binarySearch(int[] arr, int numbers) {
int low = 0;
int high = arr.length - 1;
int mid;
while (low <= high) {
mid = (low + high) / 2;
if (numbers == arr[mid]) {
return true;
}
if (numbers > arr[mid]) {
low = mid + 1;
}
if (numbers < arr[mid]) {
high = mid - 1;
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {3,5,3,2,1};
Arrays.sort(arr);
boolean b = binarySearch(arr, 2);
System.out.println(b);
}
}
public static boolean binarySearch2(int[] arr, int numbers, int low, int high) {
int mid;
if (low > high) {
return false;
}
mid = (low + high) / 2;
if (numbers == arr[mid]) {
return true;
}
if (numbers > arr[mid]) {
return binarySearch2(arr, numbers, mid + 1, high);
}
if (numbers < arr[mid]) {
return binarySearch2(arr, numbers, low, mid - 1);
}
return false;
}