- 非递归
public static int binarySearch(int[] a, int x) {
int L = 0, R = a.length - 1;
while (L <= R) {
int M = (L + R) / 2;
if (a[M] > x)
R = M - 1;
else if (a[M] < x)
L = M + 1;
else
return M; //Found
}
return -1; //Not_Found is defined as -1
}