看算法,看到用基数排序来对整数进行排序时,算法复杂度为O(n)。赶脚好牛逼的样子,拿来和std::sort比比,不比不知道,一比吓一跳呀。上代码:
radix_sort.hpp
#include
template
void radix_sort (T v[],const int begin,const int end)
{
unsigned const int lp = sizeof(T)/r;
unsigned const int step = r*8;
unsigned const long size = 1<>(i*step))&mask ];
temp[j]=v[j];
}
unsigned int pos = 0;
for(int j=0;j>(i*step))&mask]++]=temp[j];
}
}
delete [] temp;
}
test.cpp
#include "radix_sort.h"
#include
#include
#include
#include
int main()
{
typedef int type;
const int N = 500000;
type *v=new type [N];
type *sv=new type [N];
int t=5;
for(int i=0;i(v,0,N);
}
{
std::cout<<"std::sort\t:";
boost::progress_timer t;
std::sort(sv,sv+N);
}
}
/*
for(int i=0;i<1000;i++){
std::cout<
结果
这是有多快,吖。。。。。