public class ArrayDemo2 {
public static void main(String[] args) {
int[] arr = { 18, 37, 54, 76, 92 };
int index = getIndex(arr, 76);
System.out.println(index);
}
public static int getIndex(int[] arr, int value) {
int maxIndex = arr.length - 1;
int minIndex = 0;
int midIndex = (maxIndex + minIndex) / 2;
while (arr[midIndex] != value) {
if (arr[midIndex] > value) {
maxIndex = midIndex - 1;
} else if (arr[midIndex] < value) {
minIndex = midIndex + 1;
}
if (minIndex > maxIndex) {
return -1;
}
midIndex = (maxIndex + minIndex) / 2;
}
return midIndex;
}
}