二分查找法。必须有前提:数组中的元素要有序。 public static int halfSeach_2(int[] arr,int key){ int min,max,mid; min = 0; max = arr.length-1; mid = (max+min)>>1; //(max+min)/2; while(arr[mid]!=key){ if(key>arr[mid]){ min = mid + 1; } else if(key<arr[mid]) max = mid - 1; if(max<min) return -1; mid = (max+min)>>1; } return mid; } |
|