public class BanarySearch {
public static void main(String[] args) {
int [] ints={1,3,12,33,44,45,56,78,90};
int index=binarySearch(ints, 77);
System.out.println(index);
}
public static int binarySearch(int[] arr, int index) {
int min = 0, max = arr.length - 1;
int mid = (min + max) >> 1;
while (arr[mid] != index) {
if (index > arr[mid]) {
min = mid + 1;
} else if (index < arr[mid]) {
max = mid - 1;
}
if (max < min)
return -1;
mid = (min + max) >> 1;
}
return mid;
}
}
public static void main(String[] args) {
int [] ints={1,3,12,33,44,45,56,78,90};
int index=binarySearch(ints, 77);
System.out.println(index);
}
public static int binarySearch(int[] arr, int index) {
int min = 0, max = arr.length - 1;
int mid = (min + max) >> 1;
while (arr[mid] != index) {
if (index > arr[mid]) {
min = mid + 1;
} else if (index < arr[mid]) {
max = mid - 1;
}
if (max < min)
return -1;
mid = (min + max) >> 1;
}
return mid;
}
}