二分查找

 

一、对有序后数组进行查找,未查找到返回 -1

public static int swap(int[] t,int x){

int mid,low,high;
low = 0;
high = t.length-1;
while(low < high){
mid = (low+high)/2;
if(t[mid] > x){
high = mid - 1;
}else if(t[mid] < x){
low =mid + 1;
}else if(t[mid] == x){
return mid;
}
}
return -1;
}

posted @ 2017-04-17 20:39  ThrownBug  阅读(185)  评论(0编辑  收藏  举报