About half, you can go to see big brother wrote this blog:
https://blog.csdn.net/CCSGTC/article/details/80586181#commentBox
Version1: sorted, the length is n, an ordered array of non-repeating elements, the array index from 0 to n-1, to find the position x.
int td(int n, int x)
{
int l=0, r=n-1, m;
while(l<=r) //左右边界交错后,说明没有元素可以查找了,这时不符合循环条件
{
m = (l+r)/2; //每次折半,看区间中间的元素
if (t[m]==x) return m; //该元素等于x,直接return,返回找到的位置
else if (t[m]<x) l = m + 1;//该元素小于x,说明x此时处在右半区间,把左边界往右边缩
else if (t[m]>x) r = m - 1;//该元素大于x,说明x此时处在左半区间,把右边界往左边缩
}
return l;
}
If so, returns the corresponding x position index; if not, there is an array of elements and greater than x, and returns the position of a first element index of greater than x; if not, and the elements in the array are less than x, returns n.