/** * * 二分查找算法 * * * * @param srcArray 有序数组 * * @param target 查找元素 * * @return srcArray数组下标,没找到返回-1 */ public int binarySearch(int[] srcArray, int target){ int left = 0; int right = srcArray.length - 1; while(left <= right) { int mid = (left + right)/2; if(srcArray[mid] == target) { return mid; } else if(srcArray[mid] > target) { right = mid - 1; } else { left = mid + 1; } } return -1; }
时间: 2024-10-14 08:51:12