使用二分法实现数据查找c语言代码
{
int order(int a[],int x);
int a[10];
int i,j,k,x,t,temp,flag;
printf("please input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("The number is:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
printf("please input a number to compare:\n");
scanf("%d",&x);
order(a,x);
getch();
}
int order(int a[],int x)
{
int m,l,h;
l=0;
h=9;
while(l-1 { m=(l+h)/2; if(x==a[m]) { printf("found it, the number is a[%d]\n",m); break; } else if(x>=a[m]) l=m+1; else if(x h=m-1; if(l-1>=h+1) printf("can't find the number!"); }
相关主题