Radix Sort
Sorted by the number of digits each, divided into LSD (least significant bit first) and MSD (highest priority) two kinds. Complexity \ (O (D (n-K +)) \) , where \ (D \) is the maximum number of bits, \ (n-\) is the number of sorting, \ (K \) to several base.
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int getdg(int x,int d)
{
while(d--) x/=10;
return x%10;
}
void radix_sort(int* begin,int* end)
{
int x=*max_element(begin,end),d=0;
int count[10], bucket[end-begin+1];
while(x) x/=10,++d;
for(int i=0;i<d;i++)
{
memset(count,0,sizeof(count));
for(int* j=begin;j!=end+1;++j)
++count[getdg(*j,i)];
for(int j=0;j<10;++j)
count[j]+=count[j-1]; //这里用来计算键值索引
for(int* j=end;j!=begin-1;--j)
bucket[--count[getdg(*j,i)]]=*j; //注意这里要从后往前循环,因为排序是按照先遍历到的排在后面,如果从前开始循环会将排好的顺序打乱
for(int *j=begin,k=0;j!=end+1;++j,++k)
*j=bucket[k]; //一定要记得再放回去
}
}
int main()
{
int br[12]={0,20,80,90,589,998,965,852,123,456,789,0};
radix_sort(br+1,br+10);
for(int i=1;i<=10;i++)
cout << br[i] << " ";
cout << endl;
return 0;
}