/** * Returns the index of the specified target in the specified array. * * @param arr the array of integers, must be sorted in ascending order * @param target the search target * @return index of key in array {@code arr} if present; {@code -1} otherwise */ publicstaticintbinarySearch(int[] arr, int target) { intlow=0; inthigh= arr.length - 1; while (low <= high) { intmid= low + (high - low) / 2; if (arr[mid] == target) { return mid; } elseif (arr[mid] < target) { low = mid + 1; } elseif (arr[mid] > target) { high = mid - 1; } }
/** * Returns the index of the specified target in the specified array. * * @param arr the array of integers, must be sorted in ascending order * @param low the low index of the interval * @param high the high index of the interval * @param target the search target * @return index of key in array {@code arr} if present; {@code -1} otherwise */ publicstaticintbinarySearch(int[] arr, int low, int high, int target) { if (low > high) { return -1; }