关于折半查找法,大家看看
#define N 10main()
{
int a[N]={1,3,5,7,9,11,13,15,17,19};
int i,j,number,low,high,mid,loca; //loca干什么的
int flag=1,sign=1;
printf("input:\n");
scanf("%d",&number);
while(flag==1) // 解释一下括号里的flag==1的目的
{
loca=1;low=0;high=N-1;sign=1;
if(number<a[0]||number>a[N-1])
loca=-1;
while((sign==1)&&(low<=high))
{
mid=(low+high)/2;
if(number==a[mid])
{
loca=mid;
printf("find %d,第%d个\n",number,loca+1);
sign=0;
flag=0;
}
else if(number<a[mid]) high=mid-1; //high=mid-1 low=mid+1什么意思
else low=mid+1;
}
if(sign==1||loca==-1)
{
printf("Not found\n");
flag=0;
}
}
}