不知道为什么时间函数不正确,显示运行时间为0(快速 插入 合并排序是正确的)
int main(){
int a[100];
int i,number;
FILE *fp;
clock_t start1,start2,start3,end1,end2,end3;
double time1,time2,time3;
srand((unsigned) time(NULL)); //用时间做种,每次产生随机数不一样
//将随机产生的10000个数放入 input.text
fp=fopen("input.txt","r+");
if(fp==NULL)
{
printf("open file failed\n");
return -1;
}
for (i=0; i<50; i++)
{
if(i%10==0&&i!=0)
fprintf(fp,"\n");
number = rand() % 101; //产生0-100的随机数
fprintf(fp,"%d ", number);
//fscanf(fp,"%d",&a[i]);
a[i] = number;
//printf("%d ",a[i]);
}
fclose(fp);
start1 = clock();
quick_sort(a,50);
end1 = clock();
time1=(double)(end1-start1)/CLOCKS_PER_SEC;
start2 = clock();
insert_sort(a,50);
end2 = clock();
time2=(double)(end2-start2)/CLOCKS_PER_SEC;
start3 = clock();
merge_sort(a,0,49);
end3 = clock();
time3=(double)(end3-start3)/CLOCKS_PER_SEC;
//将排好序的数放入output.text
fp=fopen("output.txt","w");
if(fp==NULL)
{
printf("open file failed\n");
return -1;
}
for(i=0;i<50;i++)
{
if(i%10==0&&i!=0)
fprintf(fp,"\n");
fprintf(fp,"%d ",a[i]);
}
/*fprintf(fp,"\n");
fprintf(fp,"快速排序的运行时间为%lf\n", time1);
fprintf(fp,"插入排序的运行时间为%lf\n", time2);
fprintf(fp,"合并排序的运行时间为%lf\n", time3);*/
fclose(fp);
return 0;
}