qsort效率探究

#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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值