Key points
The base sort is also called the bucket sort .
The cardinality sort differs from the seven sorting methods explained earlier in this series, and it does not need to compare the size of the keywords.
It is sorted by "allocation" and "collection" of the ordered n elements according to the values of each of the keywords.
Here's a concrete example of how the cardinality sort is done.
There is an initial sequence of: R {50, 123, 543, 187, 49, 30, 0, 2, 11, 100}.
We know that in any Arab number, the cardinality of its various digits is expressed in 0~9.
So we might as well think of 0~9 as 10 barrels.
We classify the number of single digits of a sequence and divide it into a specified bucket. For example: r[0] = 50, single digit is 0, this number is stored in the bucket numbered 0.
After sorting, we take all the numbers out of each bucket, sequentially, in order from number 0 to number 9.
At this point, the resulting sequence is a sequence of incremental trends on a single digit.
In single-digit order: {50, 30, 0, 100, 11, 2, 123, 543, 187, 49}.
Next, you can sort the 10-digit and hundred-digit numbers in this way, and finally get the sorted sequence.
The code is as follows:
(1) LSD method realization
Implementation code
PublicclassRadixsort {
//Get the number on the D digit of this number x
//For example, get 123 1 digits, and the result returns 3
PublicintGetdigit (intXintD) {
intA[] = {1, 1, 10, 100};//The maximum number in this example is the number of hundred, so as long as the 100 is available.
return((X/a[d])% 10);
}
Public voidRadixsort (int[] list,intBeginintEndintDigit) {
FinalintRadix = 10;//cardinality
inti = 0, j = 0;
int[] Count =Newint[Radix];//number of data statistics stored in each bucket
int[] Bucket =Newint[End-begin + 1];
//perform the sorting process in order from low to high
for(intD = 1; d <= Digit; d++) {
//data statistics for empty buckets
for(i = 0; i < radix; i++) {
Count[i] = 0;
}
//count the number of data each bucket will be loaded into
for(i = begin; I <= end; i++) {
j = Getdigit (List[i], D);
count[j]++;
}
//Count[i] Indicates the right boundary index of the first bucket
for(i = 1; i < radix; i++) {
Count[i] = Count[i] + count[i-1];
}
//load data sequentially into a bucket
//This is a right-to-left scan to ensure sort stability.
for(i = end; I >= begin; i--) {
j = Getdigit (List[i], D);//figure out the k digits of the key code, for example: the 3rd digit of 576 is 5
BUCKET[COUNT[J]-1] = list[i];//into the corresponding bucket, count[j]-1 is the right boundary index of the J-Bucket.
count[j]--;//the loading Data index of the corresponding bucket is reduced by one
}
//The data in the allocated bucket is then poured out, which is already a table with an ordered number of previous digits
for(i = begin, j = 0; I <= end; i++, J + +) {
List[i] = Bucket[j];
}
}
}
Publicint[] Sort (int[] list) {
Radixsort (list, 0, list.length-1, 3);
returnList
}
//Print Complete Sequence
PublicvoidPrintall (int[] list) {
for(intValue:list) {
System.out.print (value + "\ T");
}
System.out.println ();
}
PublicStaticvoidMain (string[] args) {
int[] array = {50, 123, 543, 187, 49, 30, 0, 2, 11, 100};
Radixsort radix =NewRadixsort ();
System.out.print ("Before sorting: \t\t");
Radix.printall (array);
Radix.sort (array);
System.out.print ("After sorting: \t\t");
Radix.printall (array);
}
}
Algorithm Analysis
Performance of cardinality sorting
| Sort category |
Sorting methods |
Complexity of Time |
Complexity of space |
Stability |
Complexity |
| Average situation |
Worst case scenario |
Best case |
| Base sort |
Base sort |
O (d (n+r)) |
O (d (n+r)) |
O (d (n+r)) |
O (N+r) |
Stability |
More complex |
Complexity of Time
It is assumed that in the cardinality sort, r is the cardinality and D is the number of digits. The time complexity of the cardinality sort is O (d (n+r)).
We can see that the efficiency of the cardinality ordering is not correlated with the order of the initial sequence.
Complexity of space
During the cardinality sort process, a n+r is required for a "barrel" operation on the cardinality of any number of digits.
Algorithm stability
In the Cardinal sort process, each time the elements of the same number on the current digit are uniformly "loaded", there is no need to swap positions. So the Cardinal sort is a stable algorithm.
Sort eight cardinal sort