我眼中的快速排序.
基本上在这个版块里,每隔一个月,就会有一个讨论排序算法的主题飘上来.
几乎是每个月都来的东西了.
似乎这个月没来,不能破坏这个传统,是吧.
^_^
反正这个我只是当成学习笔记来记的.
喜欢的随便看看吧.
PS:
后面的代码有部分是网上不知道哪里找来的,年代久远了,已经找不到连接的.
只能对原作者说抱歉了.
不废话了,以下是检测函数用的代码.
几乎是每个月都来的东西了.
似乎这个月没来,不能破坏这个传统,是吧.
^_^
反正这个我只是当成学习笔记来记的.
喜欢的随便看看吧.
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;
}
...全文
相关推荐
快速排序C++
在提供的C++代码中,快速排序的实现分为以下几个关键部分:1.
产品经理眼中的市场.pdf
产品经理眼中的市场产品经理眼中的市场产品经理眼中的市场产品经理眼中的市场
JAVA实现快速排序
采用这种方法得到的快速排序一般称为随机的快速排序。
快速排序 java代码
"该资源提供了一个使用Java实现的快速排序算法。代码可以在Java环境中运行,对一个整数列表进行排序。快速排序是一种高效的排序算法,它的基本思想是通过一趟排序将待排记录分隔成独立的两部分,其中一部
快速排序的三种写法及随机化快速排序
文件名列表中的"快速排序1.txt"至"快速排序3.txt"可能包含了这三种快速排序方法的实现代码或详细解释。在学习这些文件时,你可以关注如何选择主元、如何进行分区操作以及如何进行递归调用等关键步骤。


