1. Straightforward
Base sorting is based on a sort of multiple keywords, with the help of "allocation" and "collection" two processes to get an ordered sequence, its time complexity is O (d (n+rd)), the space complexity is O (N+RD), is a stable sort ...
For example, join a list of #->278->109->63->930->589->184->505->269->8->83 (#为头节点, other data nodes)
Sequence, we can easily get information:
Rd:rd here refers to the decimal 10, that is, RD = ten (but, if it is in an alphabetical sequence, for example, #-> ' C ', ' A ', ' B ', ' F ' here Rd refers to the 26 binary, that is, RD = 26)
D:d here refers to the maximum number of bits in the sequence, it is easy to get the maximum value in this sequence is 930, is a three-digit number, that is, d = 3
2. Process Introduction
Next, to sort the column as:
#->278->109->63->930->589->184->505->269->8->83
Next, we need to start the first allocation (in single-digit allocation), the establishment is the RD queue or the RD list (the following 0,1,2 .... 9 are linked list numbers):
Note: According to the single-digit distribution means: For example, 278 bits is 8, put it into the 8th chain list, 109 of the bits is 9, put it into the list of the 9th number;
0->930
1
2
3->63->83
4->184
5->505
6
7
8->278->8
9->109->589->269
After allocation, start the first trip collection (collected here is from the chain No. 0 list to the end of the 9th linked list):
#->930->63->83->184->505->278->8->109->589->269
Second allocation (by 10-bit allocation):
0->505->109->8
1
2
3->930
4
5
6->63->269
7->278
8->83->184->589
9
Second Trip collection:
#->505->109->8->930->63->269->278->83->184->589
Third trip allocation (by hundred):
0->8->63->83
1->109->184
2->269->278
3
4
5->505->589
6
7
8
9->930
Third collection:
#->8->63->83->109->184->269->278->505->589->930
Sort done ...
3. In fact, the idea is that, but the code varies from person to person, take what data structure to achieve can achieve different results, but the results can be ordered, the following reference code
1#include <bits/stdc++.h>2 #defineSIZE 0x323 #defineRd 104 using namespacestd;5typedefstructsnode{6 intkey;7 structSnode* Next;//Lower Pointer8} *Sradix;9typedefstructbnode{TenSradix D[size];//Maximum length One intLength//actual length A } Bradix; - intA[] = {1,Ten, -, +,10000,100000,1000000,10000000,100000000,1000000000};//Auxiliary Subscript Array - intGetPos (intValueintPOS);//Get subscript the voidRadixsort (Bradix &rl);//Base Sort - voidPRINTL (Bradix RL);//Print Collection List - voidPRINTC (Bradix R);//Print allocation Process - intMain () { +Bradix RL;//collecting the main list -Bradix LA;//Sports Chain List +cout <<"----------- 1. Base Order- ---------\n"; Acout <<"Please enter the length of the sequence to be sorted: \ n"; atCIN >>rl.length; -cout <<"Initialize Collection table ..."; -Rl.d[rl.length] =NewSnode; -Rl.d[rl.length]->next =NULL; -La =RL; -cout <<"Please enter a collection of elements for the sequence: \ n"; in for(inti =0; i < rl.length; i++) {//initializing the collection chain list -Sradix p =NewSnode; toCIN >> P->key; +P->next = la.d[rl.length]->Next; -La.d[rl.length]->next =p; theLa.d[rl.length] =p; * } $ Radixsort (RL);Panax Notoginsengcout <<"the sequence after the order is entered as follows ... \ n"; - PRINTL (RL); the return 0; + } A intGetPos (intValueintPOS) { the returnValue/a[pos-1] %Ten; + } - voidRadixsort (Bradix &RL) { $ intMaxValue =0, d =1; $ Bradix R; -R.length =rl.length; -R.d[r.length] =Rl.d[rl.length]; the while(r.d[rl.length]->next) { - if(R.d[rl.length]->next->key >maxValue)WuyiMaxValue = r.d[rl.length]->next->key; theR.d[rl.length] = r.d[rl.length]->Next; - } Wu for(inti =1; i < Rd; i++) - if(Maxvalue/a[i] = =0) Break; About Elsed++; $ for(inti =1; I <= D; i++){ -printf"---------- %d times allocation- --------\n", i); - Bradix R2, R0; -R0.length = R2.length =rl.length; AR0.d[r0.length] = R2.d[r2.length] =Rl.d[rl.length]; + for(intK =0; K < Rl.length; k++){ theR0.D[K] = r2.d[k] =NewSnode; -R0.d[k]->next = R2.d[k]->next =NULL; $ } the while(r0.d[r0.length]->next) { the intKey = R0.d[rl.length]->next->key; the intpos =GetPos (key, i); the for(intj =0; J < Rd; J + +){ - if(pos = =j) { inSradix p =NewSnode; theP->key =key; theP->next = r0.d[j]->Next; AboutR0.d[j]->next =p; theR0.D[J] =p; thej =Rd; the } + } -R0.d[r0.length] = r0.d[r0.length]->Next; the }Bayi PRINTC (R2); the for(intj =0; J < Rd; J + +){ the if(R2.d[j]) { - while(r2.d[j]->next) { - intKey = R2.d[j]->next->key; theR2.d[rl.length]->next->key =key; theR2.D[J] = r2.d[j]->Next; theR2.d[rl.length] = r2.d[rl.length]->Next; the } - } the } the } thecout <<"- ---------end of sort---------\n";94 } the voidPrintl (Bradix RL) { the while(rl.d[rl.length]->next) { thecout << Rl.d[rl.length]->next->key <<" ";98Rl.d[rl.length] = rl.d[rl.length]->Next; About } -cout <<Endl;101 }102 voidPrintc (Bradix r) {103 for(inti =0; i < Rd; i++){104printf"# (%d)", i); the if(R.d[i]) {106 while(r.d[i]->next) {107cout <<" -"<< r.d[i]->next->key;108R.d[i] = r.d[i]->Next;109 } the }111cout <<Endl; the }113}
The following (including the above allocation and collection process):
4. Please leave a comment in the comments section below, I will try to answer for everyone ... I hope you will support
Base sort < lowest bit priority > list implementation