关于C++各类排序算法与std::sort性能的比较

talk is cheap.直接放代码(C++)。
先定义一个计时的类。

class Time_count//时间计时类,需要#include< ctime>
{
private:
    clock_t start_,end_;
    double duration;//记录时间
public:
    Time_count()
    {
        start_=clock();
    }
    void show_time()
    {
        end_=clock();
        duration = (double)(end_-start_)/CLOCKS_PER_SEC;
        cout< < "spend time "< < duration< < " seconds"< < endl< < endl;
    }
};

然后,计时的时候,实例化一个Time_count类,再通过show_time()成员函数获得时间。这里不细讲。

define LENGTH 10000//排序长度为10000的数组

注意 如果想改变数组长度,只需改变LENGTH大小即可。
开头就这么定义:

 int a[LENGTH+10];
    srand(time(NULL));
    for(int i=0;i< LENGTH;i++)
    {
        a[i]=rand()%10000+1;//随机生成1-10000之间的数字填充进数组
    } 

然后是驱动函数部分:
/*1.以下为插入排序/

 Time_count t1;
    insertion_sort(a);
    cout< < "1.after insertion sort:"< < endl;
    t1.show_time();
    //print(a);
    /**2.以下为冒泡排序*/
    Time_count t2;
    bubble_sort(a);
    cout< < "2.after bubble sort:"< < endl;
    t2.show_time();
    //print(a);
    /**3.以下为选择排序*/
    Time_count t3;
    selection_sort(a);
    cout< < "3.aft
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值