(1)
int Binary_Search(SeqList L, int key){
//在有序表L中查找关键字为key的元素。若存在则返回其位置,不存在则返回-1。
int low = 0, high = L.Tablelen - 1, mid;
while(low <= high){
mid = (low + high) / 2; //取中间位置
if(L.elem[mid] == key)
return mid; //查找成功则返回所在位置
else if(L.elem[mid] > key)
high = mid - 1; //从前半部分继续查找
else
low = mid + 1; //从后半部分继续查找
}
return -1;
}
(2)
int Binary_Search(int a, int kx, int length){
int mid, low, high, flag = 0;
low = 0; high = length;
while(low <= high){
mid = (low + high) / 2;
if(kx < a[mid]) high = mid - 1;
else if(kx > a[mid]) low = mid + 1;
else{
flag = mid;
break;
}
}
return flag;
}