Archlinux
GCC 13.1.1 20230429
2023-07-29 11:07:02 星期六
点击查看代码
#include<stdio.h>
int binsearch( int x, int v[], int n )
{
int low, high, mid;
low = 0;
high = n-1;
mid = ( low + high )/2;
while((low <= high) && ( v[mid] != x ))
{
if( x<v[mid] )
high = mid - 1;
else
low = mid + 1;
mid = ( low + high )/2;
}
if( x == v[mid] )
return mid;
else
return -1;
}
int main()
{
int c_in;
int arr[] = { 1, 2, 4, 8, 16, 32 };
c_in = binsearch( 8, arr, 7 );
printf("%d\n", c_in);
return 0;
}
运行截图:
输出正确,就不比较了。
小白刚学习C语言,代码质量不高,欢迎评论。
标签:int,high,while,循环,low,测试,执行 From: https://www.cnblogs.com/yuwu/p/17589505.html