Principle
The Cardinal sort (Radix sort) is a non-comparative integer sorting algorithm that cuts the number of integers by bits into different numbers and compares them by each bit. Because integers can also express strings (such as names or dates) and floating-point numbers in a particular format, the cardinality sort is not only used for integers.
Unify all the values to be compared (positive integers) to the same digit length , with a short number of digits preceded by 0 . Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.
Efficiency
The time complexity of the radix sort is O (k N), where n is the number of ordered elements, and K is the number of digits. Note that this is not to say that this time complexity must be better than O (n log (n)), the size of K depends on the choice of digital bits and the size of the complete set of data types to be sorted, K determines how many rounds are processed, and N is the number of operations per round.
Base sort basic operations are less expensive, k is generally less than logn, so cardinality sorting is generally faster than sorting based on comparisons , such as fast sorting.
Worst-of-space complexity is O (k N)
Java implementation
Now there are arrays: 278,109,63,930,589,184,505,269,8,83. Divide the array into 10 lists according to the number of members (of course some of the linked lists may not contain elements)
first time allocation :
0:930
1:
2:
3:63,83
4:184
5:505
6:
7:
8:278,8
9:109,589,269
the first collection of arrays :
930,63,83,184,505,278,8,109,589,269
Second allocation :
0:505,8,109
1:
2:
3:930
4:
5:
6:63,269
7:278
8:83,184,589
9:
after the second collection of arrays :
505,8,109,930,63,269,278,83,184,589
Third allocation:
0:8,63,83
1:109,184
2:278,269
3:
4:
5:505,589
6:
7:
8:
9:930
Finally get the sequence:
8,63,83,109,184,269,278,505,589,930
The cardinal order is to use the multi-keyword first to achieve local order, and then adjust to achieve global order.
Code implementation:
Public classTest { Public Static voidMain (string[] args) {int[]Array= {278,109, the,930,589,184,505,269,8, the}; Radixsort (Array); for(DoubleA:Array) {System.out.println (a); } } Public Static voidRadixsort (int[]Array){//------------------------------------------determine the number of trips to sort---------------------------------- intmax=Array[0]; for(intI=1;i<Array. length;i++) {if(Array[I]>max) {max=Array[i]; } }intTime=0; while(max>0) {max/=Ten; time++; }//----------------------------------------Initialize the 10 linked list when the user is assigned to a staging-------------------------------List<list<integer>>List=NewArraylist<list<integer>> (); for(intI=0;i<Ten; i++) {list<integer> item=NewArraylist<integer> ();List. Add (item); }//-----------------------------------------for time allocation and collection------------------------------------- for(intI=0; i<time;i++) {//allocation elements; for(intj=0;j<Array. length;j++) {intindex =Array[J]% (int) Math.POW(Ten, i+1)/(int) Math.POW(Ten, i);List. Get (Index). Add (Array[j]); }//collect elements; intCount=0; for(intk=0;k<Ten; k++) {if(List. Get (k). Size () >0){ for(intA:List. Get (k)) {Array[Count]=a; count++; }//Clear data for the next collection List. Get (k). Clear (); } } } }}
Operation Result:
[Data structure] cardinality sorting