java二分查找法
1 public static int binarySearch(int[] srcArray, int des) { 2 int low = 0; 3 int high = srcArray.length - 1; 4 while ((low <= high) && (low <= srcArray.length - 1) && (high <= srcArray.length - 1)) { 5 int middle = (high + low) / 2; 6 if (des == srcArray[middle]) { 7 return middle; 8 } else if (des < srcArray[middle]) { 9 high = middle - 1; 10 } else { 11 low = middle + 1; 12 } 13 } 14 return -1; 15 } 16 17 public static int binarySearch2(int[] srcArray, int val) { 18 int low = 0; 19 int high = (srcArray.length) - 1; 20 while (low <= high) { 21 int mid = (low + high)/2; 22 if (srcArray[mid] == val) { 23 return mid; 24 } else if (srcArray[mid] > val) { 25 high = mid - 1; 26 } else { 27 low = mid + 1; 28 } 29 } 30 return -1; 31 }