Binary search
public class Main {
public static void main(String[] args) {
int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
int x = search(a, 12);
System.out.println(x);
}
private static int search(int[] src, int val) {
int min = 0, max = src.length - 1;
int mid;
while (true) {
mid = (min + max) / 2;
if (src[mid] == val) {
return mid;
} else if (min > max) {
return -1;
} else
{
if (src[mid] < val)
min = mid + 1;
else
max = mid - 1;
}
}
}
}