性能测试:
#include <iostream>
#include <set>
#include <cstdio>
#include <ctime>
#include <algorithm>
//typedef long long ll;
#define ll long long
#define MAXN 1000009
using namespace std;
struct box_Sort
{
int score;
string name;
}my_Box[100];
int a[1000009];
void bubble_Sort(int scale)
{
for(int i = 0; i < scale; ++i)
for(int j = 0; j < scale - i; ++j)
if(a[j] > a[j + 1])
swap(a[j],a[j + 1]);
}
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void box_sort(int a[MAXN])
{
int num;
int t;
cin >> num;
while(num--)
{
cin >> t;
a[t] ++;
}
for(int i = 0; i < MAXN; ++i)
if(a[i])
cout << i << ' ';
}
void bubble_sort(int a[MAXN])
{
for(int i = 0; i < MAXN - 1; ++i)
for(int j = 0; j < MAXN - 1 - i; ++j)
if(a[j] > a[j + 1]) // descent descend ascent ascend
swap(a[j], a[j + 1]);
}
void quick_sort(int left, int right)
{
if(left > right)
return ;
int temp = a[left];
int i = left, j = right;
while(i != j)
{
while(a[j] >= temp && j > i)
j--;
while(a[i] <= temp && i < j)
i++;
if(i < j)
{
swap(a[i], a[j]);
}
}
a[left] = a[i];
a[i] = temp;
quick_sort(left, i - 1);
quick_sort(i + 1, right);
return ;
}
int main(){
time_t start, over;
double run_time;
int i;
int scale = 100;
for(scale; scale <= 1e4; scale *= 10)
{
for(i = 0; i <= scale; i++)
a[i] = i;
start = clock();
bubble_Sort(scale);
over = clock();
printf("bubble with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
}
for(scale = 100; scale <= 1e6; scale *= 10)
{
for(i = 0; i <= scale; i++)
a[i] = i;
start = clock();
//QuickSort(0,scale);
qsort(a, scale, sizeof(int), compare);
over = clock();
printf("qsort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
}
for(scale = 100; scale <= 1e6; scale *= 10)
{
for(i = 0; i <= scale; i++)
a[i] = i;
start = clock();
sort(a,a+scale,greater<int>());
over = clock();
printf("sort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
}
for(scale = 40; scale <= 40000; scale *= 10)
{
for(i = 0; i <= scale; i++)
a[i] = i;
start = clock();
quick_sort(0, scale);
//qsort(a, scale, sizeof(int), compare);
over = clock();
printf("my_qsort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
}
cout << "Mission complete";
return 0;
}
输出如下:
bubble with 100 scale lever costs 0.000000 s
bubble with 1000 scale lever costs 0.003000 s
bubble with 10000 scale lever costs 0.308000 s
qsort with 100 scale lever costs 0.000000 s
qsort with 1000 scale lever costs 0.001000 s
qsort with 10000 scale lever costs 0.001000 s
qsort with 100000 scale lever costs 0.012000 s
qsort with 1000000 scale lever costs 0.153000 s
sort with 100 scale lever costs 0.000000 s
sort with 1000 scale lever costs 0.001000 s
sort with 10000 scale lever costs 0.002000 s
sort with 100000 scale lever costs 0.033000 s
sort with 1000000 scale lever costs 0.370000 s
my_qsort with 40 scale lever costs 0.000000 s
my_qsort with 400 scale lever costs 0.001000 s
my_qsort with 4000 scale lever costs 0.049000 s
my_qsort with 40000 scale lever costs 4.790000 s
Mission complete
关于时间测量:
我们使用头文件 ctime
来包含我们所需要的函数,完成计数。
time_t start, over;
double run_time;
start = clock();
function();
over = clock();
printf("my_qsort with %d scale lever costs %lf s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
time_t start, over
;time_t 是在中的一个结构体。
start = clock()
调用这样的语句,在一段我们需要计数的函数体两端,减去之后即可得到一个点数。
但想得到用秒为单位的时间,需要先将其转化为double类型,然后与CLOCKS_PER_SEC作比,就可以得到最后的秒数了。
总结:
根据数据量的增加, 我们不难发现:
qsort > sort > my_qsort = bubble
考虑原因:qsort 比 sort 更快是由于优化问题,sort并没有采用最佳最高效的排序策略,但其实也够用了。如果非要追求效率,那么使用qsort。