Algorithm description
Multi-Keyword Ordering:
Another example is 123, the theme is 1, in is 3.
123,46,791. Follow the secondary bit priority: 791,123,46
Secondary bit: 123,46,791
Sub-secondary: 46,123,791
Specific implementation
Create a bucket element node, which is implemented with a linked list.
Build the bucket tail and end node structure.
Constructs a getdigit ( int X, int D) function to get the number of D digits of X
Construction Lsdradixsort (ElementType a[], int N)
{
Initialize each bucket as an empty list
Put the original sequence in reverse order into the list of initial lists
Sort
{
Each bit of data is processed in a loop
{
Gets the current bit number of the current element
Remove from List
Insert B[di] # bucket Tail
}
}
Collection (The order of elements for each bucket is collected in pour list into a[] and release
1 #defineMaxdigit 42 #defineRadix 103typedefstructNode *Ptrtonode;4 structNode5 {6 intkey;7 Ptrtonode Next; 8 };9 //defining bucket head nodesTen structHeadnode One { A Ptrtonode head; - Ptrtonode tail; - }; thetypedefstructHeadnode Bucket[radix]; - /* - x is the number you are seeking, X is a few digits D is a few - */ + intGetdigital (intXintD) - { + intI,d;//d is the return value, which is the specific number of x on a bit A for(i=1; i<=d;i++) at { -D=x%radix;//Radix =10; -X/=radix;//Radix =10; - } - returnD; - } in voidLsdradixsort (ElementType a[],intN) - { to Bucket B; + intI, D,di; -Ptrtonode p,temp,list=NULL; the * /*Initialize bucket*/ $ for(i=0; i<radix;i++)Panax Notoginseng -B[i].head=b[i].tail=NULL; the + /*import an array in reverse order to a list of linked lists*/ A for(i=0; i<n;i++) the { +temp= (Ptrtonode)malloc(sizeof(structNode));//New Temp -temp->key=A[i]; $temp->next=list;//The first temp is null for the list list $List=temp;//Update List to temp - } - the /*starting from the lowest bit into the bucket*/ - Wuyi for(d=1;D <=maxdigit;d++) the { -p=list; Wu while(P) - { AboutDi=getdigital (p->key,d); $ /*get the number of bits*/ -temp=P; -P=p->Next; -temp->next=NULL; A /*Remove from List*/ + if(b[di].head==NULL) theB[di].head=b[di].tail=temp; - Else $ { theb[di].tail->next=temp; theB[di].tail=temp; the } the } - in /*Collect*/ thelist=NULL; the for(di=radix-1;D i>=0;D i--) About { the if(B[di].head) the { theb[di].tail->next=list; +list=B[di].head; -B[di].head=b[di].tail=null;//emptying the bucket the }Bayi } the } the /*pour list into a[] and free up space*/ - for(i=0; i<n;i++) - { thetemp=list; theList=list->Next; theA[i]=temp->key; the //printf ("I:%d A:%d", i,a[i]); - Free(temp); the } the}
Error Analysis: 1. Put 82 rows of {in 70 rows.
Cardinal sort-secondary bit first algorithm