#include <iostream> #include <cstdlib>using namespace std;int getdigit (int x,int d) {int a[]={1,1,10 };//since the maximum number of arrays to be sorted is two digits, it is good to return ((x/a[d)%10) to 10 bits;//Determine the bucket number}void printarr (int ar[],int n) {for (int i=0;i<n;i++) cout<<ar[i]<< ""; Cout<<endl;} void Msdradix_sort (int arr[],int begin,int end,int d) {const int radix=10; int count[radix],i,j; Empty for (i=0;i<radix;++i) {count[i]=0; }//Allocate bucket storage space int *bucket= (int *) malloc ((end-begin+1) *sizeof (int)); Count the number of elements that each bucket needs to be loaded for (i=begin;i<=end;++i) {count[getdigit (arr[i],d)]++; }//To find a convenient index of the bucket, Count[i] value is the right boundary index of the I-bucket +1 for (i=01;i<radix;++i) {count[i]=count[i]+count[i-1]; }//Right-to-left scanning, to ensure the stability of the order for (i=end;i>=begin;--i) {j=getdigit (arr[i],d);//Find out the key code of the D-digit, such as the third digit of 576 is 5 b ucket[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] is the left edge of the I bucket//collect data from each bucket for (i=begin,j=0;i<=END;++I,++J) {arr[i]=bucket[j]; }//Free storage space (bucket); Reorder the data in each bucket for (i=0;i<radix;i++) {int p1=begin+count[i];//the left boundary int p2=begin+count[i+1]-1;//of the first bucket The first bucket has the boundary if (p1<p2&&d>1) {msdradix_sort (arr,p1,p2,d-1);//The first bucket is recursively called, the cardinality is sorted, and the digits fall by 1. }}}int Main () {int ar[]={12,24,54,23,54,76,97,45,23,65,87,34,}; int len=sizeof (AR)/sizeof (int); cout<< "pre-sort data as follows" <<endl; Printarr (Ar,len); Msdradix_sort (ar,0,len-1,2); cout<< "After sorting results as follows:" <<endl; Printarr (Ar,len);}
The highest-bit precedence method is usually a recursive process:
<1> first according to the highest bit key code K1 sorting, get a number of object groups, object group each object has the same key code K1.
<2> then sorts the objects in each group according to the key code K2, and then divides them into smaller subgroups, each of which has the same K1 and K2 values as the K2 value.
<3> repeat until the key-code KD finishes sorting.
<4> Finally, the objects in all subgroups are concatenated in turn to get an ordered sequence of objects.
From[http://www.cnblogs.com/braveliu/archive/2013/01/21/2870201.html]
Base-ordered MSD implementation C + +