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