#include <iostream> #include <cstdlib>using namespace Std;const int maxsize=10000;int getdigit (int x,int d) { int a[]={1,1,10,100}; Return (X/a[d])%10;} void Printarr (int ar[],int n) {for (int i=0;i<n;++i) {cout<<ar[i]<< ""; } Cout<<endl;} void Lsdradix_sort (int arr[],int begin,int end,int d) {const int radix=10; int count[radix],i,j; int *bucket= (int*) malloc ((end-begin+1) *sizeof (int));//The space of all buckets is opened//sorted according to the allocation criteria for (int k=1;k<=d;++k) { Empty for (i=0;i<radix;++i) {count[i]=0; }//Count the number of data in each bucket for (i=begin;i<=end;++i) {count[getdigit (arr[i],k)]++; }//count[i] represents the right boundary index for the first bucket for (i=1;i<radix;i++) {count[i]=count[i]+count[i-1]; }//load the data sequentially into the bucket (pay attention to the allocation technique when loading) for (i=end;i>=begin;--i)//right-to-left scan, guaranteed sort stability {j=getdigit (arr[i],k) //Find out the k digits of the key code, for example, the 3rd digit of 576 is 5 bucket[count[j]-1]=arr[i];//into the corresponding bucket, count[j]-1 is the right boundary index of the J-Bucket--count[j]; }//At this time count[i] for the I bucket left boundary//collect data from each bucket for (i=begin,j=0;i<=end;++i,++j) {Arr[i]=buck ET[J]; }} free (bucket);} int main () {int br[10]={23,34,54,67,3,76,2,87,25,76}; cout<< "Meta data is as follows:" <<endl; Printarr (br,10); Lsdradix_sort (br,0,9,3); cout<< "sorted data as follows:" <<endl; Printarr (br,10);}
The lowest bit priority method first sorts all the objects according to the lowest bit key KD,
Then according to sub-low key code Kd-1 the results of the previous trip sorted again,
Repeat until the last order is completed according to the key code K1, and an ordered sequence can be obtained.
When you sort each key code using this sort method, you do not need to group again, but the entire group of objects.
From[http://www.cnblogs.com/braveliu/archive/2013/01/21/2870201.html]
Cardinal sort of LSD implementation C + +