int search(int *nums,int numsLen,int target)
{
int low = 0;
int high = numsLen - 1;
int mid = (low + high)/2; //设置中间值
if(numsLen<=0)
return -1;
while(target != nums[mid] && low <= high)
{
if(target > nums[mid]) //要找的元素大于中间的元素
{
low = mid + 1; //让低下标等于中间值
mid = (low + high)/2; //重新设置中间值
}
else if(target < nums[mid]) //要找的元素小于中间的元素
{
high = mid - 1; //让高下标等于中间值
mid = (low + high)/2; //重新设置中间值
}
}
if(low > high) //没找到
return -1;
else
return mid; //找到了,返回其下标
}