我眼中的快速排序.

knate knate 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
基本上在这个版块里,每隔一个月,就会有一个讨论排序算法的主题飘上来.
几乎是每个月都来的东西了.
似乎这个月没来,不能破坏这个传统,是吧.
^_^

反正这个我只是当成学习笔记来记的.
喜欢的随便看看吧.

PS:
后面的代码有部分是网上不知道哪里找来的,年代久远了,已经找不到连接的.
只能对原作者说抱歉了.

不废话了,以下是检测函数用的代码.


#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <ctime>
#include <algorithm>

using namespace std;

typedef unsigned int VT;
typedef unsigned int* pVT;
typedef unsigned int SIZE_T ;

template<class T>
void swap_kn(T & a,T & b){
T _t = a;
a = b;
b = _t;
};


typedef unsigned __int64 CTIME;

inline unsigned __int64 GetCycleCount()//测量时间的计数器,返回的是计数值,不是绝对时间值.

{

__asm _emit 0x0F

__asm _emit 0x31

}

void test_time_funtion(void (* func)(pVT,pVT),pVT pBeg,pVT pEnd ,const string & s,SIZE_T N){
if(pEnd <= pBeg + 1){
cout<<"数据错误,停止测试"<<endl;
return ;
}

CTIME t = GetCycleCount();
const CTIME CPU_FREQUENCY = 2600000;//根据自己CPU频率自己调整
SIZE_T max = N;
for(max /= pEnd - pBeg;max ;max --)
func(pBeg,pEnd);
t = GetCycleCount() - t;
cout<<s<<" 花费时间"<<t / CPU_FREQUENCY<<"毫秒"<<endl;
}

void check(pVT pDest,pVT pBeg,pVT pEnd){
for(;pBeg < pEnd;){
if(* pBeg ++ != * pDest ++){
cout<<"算法不正确"<<endl;
return ;
}
}
cout<<"算法正确"<<endl;
}


void init(pVT pDest,pVT pBeg,pVT pEnd){
for(;pBeg < pEnd;)
* pDest ++ = * pBeg ++;
}




int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned int) time(NULL));
SIZE_T n = 100000000;
SIZE_T N = 100000000;
pVT v,v1,v2;
v = new VT[n];
v1 = new VT[n];
v2 = new VT[n];

for(SIZE_T x = 0; x < n; ++ x){
v[x] = rand() * 65536 + rand();//随机数据
//v[x] = (rand() * 65536 + rand()) % 1000;//大量重复数据
//v[x] = n - x;//逆序
//v[x] = x;//正序(即已经排序好的)
}
init(v1,v,v + n);
test_time_funtion(sort,v1,v1 + n,"标准库sort",N);

//init(v2,v,v + n);//100000
//test_time_funtion(insert_sort,v2,v2 + n,"insert_sort",N);
//check(v2,v1,v1 + n);

//init(v2,v,v + n);
//test_time_funtion(sort_kn_base,v2,v2 + n,"sort_kn_base",N);
//check(v2,v1,v1 + n);

//init(v2,v,v + n);
//test_time_funtion(sort_kn_base1,v2,v2 + n,"sort_kn_base1",N);
//check(v2,v1,v1 + n);

//init(v2,v,v + n);
//test_time_funtion(sort_kn_base2,v2,v2 + n,"sort_kn_base2",N);
//check(v2,v1,v1 + n);

//init(v2,v,v + n);
//test_time_funtion(sort_kn_base3,v2,v2 + n,"sort_kn_base3",N);
//check(v2,v1,v1 + n);

//init(v2,v,v + n);
//test_time_funtion(sort_kn_1,v2,v2 + n,"sort_kn_1",N);
//check(v2,v1,v1 + n);

init(v2,v,v + n);
test_time_funtion(sort_kn_2,v2,v2 + n,"sort_kn_2",N);
check(v2,v1,v1 + n);
delete v;
delete v1;
delete v2;
return 0;
}
...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
knate
数据结构与算法 33,026 成员 35,334 内容