#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define N 200000
double order(int a[],int num,int n)//顺序查找函数
{
clock_t start,end;
double time;
int i;
start=clock();
for(i=0;i<n;i++)
{
if(a[i]num)
{
printf(“第%d个数字为所查找的数字\n”,a[i]);
break;
}
}
if(in)
{
printf(“顺序查找未找到指定数据\n”);
}
end=clock();
time=(double)(end-start)/CLOCKS_PER_SEC;
return time;
}
double middle(int a[],int num,int n)//二分查找函数
{
clock_t start,end;
double time;
int low=0,mid,high=n-1;
start=clock();
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]<num)
{
low=mid+1;
}
else if(a[mid]>num)
{
high=mid-1;
}
else
{
printf(“第%d个数字为所查找的数字\n”,a[mid]);
break;
}
}
if(low>high)
{
printf(“二分查找未找到指定数据\n”);
}
end=clock();
time=(double)(end-start)/CLOCKS_PER_SEC;
return time;
}
int main()
{
int n,num;
int i,j,temp,a[N];
char name;
FILE *fp;
printf(“请输入想查找的文件路径:”);
scanf("%c",&name);
printf(“请输入该数组所含数字量:”);
scanf("%d",&n);
if((fp=fopen("name","r"))==NULL)//从文件中读取随机数到数组中
{
printf("error\n");
exit(0);
}
for(i=0;i<n;i++)
fscanf(fp,"%d ",&a[i]);
fclose(fp);
for(i=1;i<n;i++)//将数组进行冒泡排序
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("请输入要查找的数字:");
scanf("%d",&num);
printf("顺序查找运行时间为:%f\n",order(a,num,n));
printf("二分查找运行时间为:%f\n",middle(a,num,n));
return 0;
}