像这样的代码,在第 44 页。
int binsearch(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while(low <= high){
mid = (high+low)/2;
if(x < v[mid]){
high = mid + 1;
}else if(x > v[mid]){
low = mid + 1;
}else{
return mid;
}
printf("mid is %d\n",mid);
}
return -1;
}
int main(void)
{
int v[] = {2,3,4,7,8,23,54,65,76};
int ret = binsearch(7, v, sizeof(v)/sizeof(int));
printf("%d,ret is %d\n", sizeof(v),ret);
return 0;
}
编译运行,结果死循环!所以行“低=中+ 1;” 应该取为“low = mid;”,对吗?thx。