1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package p; // 14, 17, 14, 38
public class A {
public static int commonBinarySearch(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;
if (key < arr[low] || key > arr[high] || low > high) {
return -1;
}
if (arr[(low + high) / 2] == key) {
return (low + high) / 2;
}
while (low <= high && arr[(low + high) / 2] != key) {
if (arr[(low + high) / 2] > key) {
high = (low + high) / 2 - 1;
} else if (arr[(low + high) / 2] < key) {
for (; isNoOutOfBound(arr, (low + high) / 2) && arr[(low + high) / 2] < key; ++low)
;
}
}
if (isNoOutOfBound(arr, (low + high) / 2))
return arr[(low + high) / 2];
else
return -1;
}
static boolean isNoOutOfBound(int[] arr, int index) {
return index >= 0 && index < arr.length;
}
}
|