折半查找

#include<stdio.h>
int main()
{
int i;
int low=0;
int high=N-1;
int mid;
int k=-1;
int m;
int a[N]={-3,4,7,13,45,67,89,100,180};
printf("数组中的数据如下: \n");
for(i=0;i<N;i++)
{
printf("%4d ",a[i]);
}
printf("\n");
printf("请输入您想要查找的数字: ");
scanf("%d",&m);
while(low<=high)
{
mid=(low+high)/2;
if(m<a[mid])
{
high=mid-1;
}
else
{
if(m>a[mid])
{
low=mid+1;
}
else
{
k=mid;
break;
}
}
}
if(k>=0)
{
printf("wei zhi shi %d,zhishi %d\n",k,m);
}
else
{
printf("Not find !\n");
}
return 0;
}

posted @ 2023-05-22 12:03  不会JAVA的小袁  阅读(10)  评论(0编辑  收藏  举报