本帖最后由 施祖喜 于 2013-5-1 23:16 编辑
- class halfSearch{
- public static void main(String[] args) {
- int[] ai = {1,2,3,4,5,6,7,8};
- int index =halfsearch(ai,3);
- System.out.println(index);
- }
- public static int halfsearch(int [] arr,int key){
- int max = arr.length - 1;
- int min = 0;
- int mad =(max + min)/2;
- while (arr[mad]!= key){
- if (arr[mad]>key){
- max=mad-1;
- }else if (arr[mad]<key){
- min = mad +1;
- }
- if (max<min){
- return -1;
- }
- mad = (max +min)/2;
- }
- return mad;
- }
- }
复制代码 找了好多遍硬是没找到错误出在那里. |