#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 100000000
#define MAX 100000000
int dom[SIZE],i;
int cmp(const void *a,const void *b){
return(*(int *)a-*(int *)b);
}
int main(){
clock_t start,end1,end2;
start = clock();
srand(time(NULL));
for(i=0;i<SIZE;i++){
dom[i] = ((rand()<<15)|rand()) % MAX;
}
end1 = clock();
printf("time1=%f\n",(double)(end1-start)/CLK_TCK);
qsort(dom,SIZE,4,cmp);
end2 = clock();
printf("time2=%f\n",(double)(end2-start)/CLK_TCK);
for(i=0;i<10;i++){
printf("%d\n",dom[i]);
}
return 0;
}
我这里调用的是系统中已经有的qsort
统计结果如下:
可以看到算法所需平均时间符合nlog(n)
下面是中间统计数据:
100000
0.015000 0.046000
0.015000 0.062000
0.000000 0.046000(舍)
0.015000 0.062000
0.015000 0.046000
0.015000 0.046000
1000000
0.062000 0.468000
0.062000 0.468000
0.062000 0.468000
0.062000 0.468000
0.062000 0.468000
0.078000 0.483000
10000000
0.686000 5.460000
0.655000 5.547000
0.717000 5.600000
0.655000 5.551000
0.670000 5.527000
0.655000 5.554000
100000000
7.316000 60.699000
6.770000 59.950000
6.380000 60.574000
6.458000 60.186000
6.333000 59.903000
7.118000 81.066000(舍)
6.582000 60.436000