企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 二分查找算法 ```java /** * 二分查找,前提是数组有序 * @param arr int[] * @param key int * @return int */ private static int binarySearch(int[] arr, int key) { if (arr.length == 1) { return 0; } if (!exist(arr, key)) { return -1; } int low = 0; int high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; if (key < arr[mid]) { high = mid - 1; } else if (key > arr[mid]) { low = mid + 1; } else { return mid; } } return -(low + 1); } ```