整数排序——基数排序

    看算法,看到用基数排序来对整数进行排序时,算法复杂度为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<
结果

整数排序——基数排序_第1张图片

这是有多快,吖。。。。。

转载于:https://my.oschina.net/u/1022744/blog/148790

你可能感兴趣的:(整数排序——基数排序)