#include "stdio.h"
int Bisearch(int a[],int low,int high,int k);
int main()
{
int pos,s;
int a[10]={23,25,27,29,31,33,35,37,39,41};
printf("请输入你要查找的数\n");
scanf("%d",&s);
printf("待查找的数 %d\n",s);
pos=Bisearch(a,0,9,s);//参数传递弄错了,k和s。
if(pos==-1)
printf("没有找到该数\n");
else
printf("%d在数组中位置是%d\n",s,pos);
return 0;
}
/*int Bisearch(int a[],int low,int high,int k)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(k==a[mid])return mid;
else if(k<a[mid])high=mid-1;
else low=mid+1;
}
return -1;
}*/
int Bisearch(int a[],int low,int high,int k)
{
int mid;
if(low>high)
return -1;
else
{
mid=(low+high)/2;
if(a[mid]==k)
return mid;
if(a[mid]<k)
return Bisearch(a,mid+1,high,k);
else
return Bisearch(a,low,mid-1,k);
}
}
函数调用参数之间关系还是把握不是太好。
同时要分辨好数组下标值和数组值的区别。