c语言程序之二分法查找


#include
int main()
{
int a[]={8,13,21,28,35,41,52,63,71,81,95,101,150,164};
int mid;
int low;
int high;
low=0;
high=13;
int x=13;
do{
mid=(low+high)/2;
if(x==a[mid])
{
printf("%d查找成功,在%d号位置",x,mid);
printf("\n");
return 0;
}
else if(x high=mid-1;
else if(x>a[mid])
low=mid+1;
}while(low<=high);
printf("查找失败");
printf("\n");
return 0;
}

你可能感兴趣的:(程序)