1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static int binarySearch(int[] arr, int aimValue) { if (arr == null || arr.lenth == 0) { return -1; } int L = 0; int R = arr.length - 1; while (L <= R) { int mid = L + ((R - L) >> 1); if (arr[mid] == aimValue) { return mid; } else if (arr[mid] > aimValue) { R = mid - 1; } else { L = mid + 1; } } return -1; }
|