| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 824 人关注过本帖
标题:不知道为什么时间函数不正确,显示运行时间为0(快速 插入 合并排序是正确的 ...
只看楼主 加入收藏
杏仁巧克力
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-4-1
结帖率:85.71%
收藏
 问题点数:0 回复次数:0 
不知道为什么时间函数不正确,显示运行时间为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;
}
2016-04-07 16:27
快速回复:不知道为什么时间函数不正确,显示运行时间为0(快速 插入 合并排序是 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014636 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved