1. Overview
The cardinality sort (radix sort) is a non-comparison integer sort algorithm that cuts integers by bits to different numbers, and then compares them by each number of digits. Because integers can also express strings (such as names or dates) and floating-point numbers in a particular format, cardinality sorting is not only used for integers. The invention of Cardinal order can be traced back to the contribution of Herman He Leri in punching card watchmaking (tabulation Machine) in 1887.
Principle: All values to be compared (positive integers) are unified to the same number of digits, with a shorter number of digits preceded by 0. Then, start at the lowest bit, and then sort it one at a time. This is done from the lowest bit to the highest order, and the sequence becomes an ordered series. The time complexity of cardinality ordering is O (k n), where n is the number of sorted elements, and K is the number of digits.
Understanding: Similar to the "classical algorithm" eighth back: bucket sort, here always need 10 barrels, many times, first in a single digit of the value of the bucket, that is, 1 digits into 1th barrels, 9 to put into 9th barrels, and then a 10-digit bucket sorting, and so on.
If the array is to be sorted [62,14,59,88,16] simple point five digits, allocated 10 barrels, barrel number 0-9, in single-digit numbers for the bucket number into the bucket, into the bottom of this
| 0 | 0 | 62 | 0 | 14 | 0 | 16 | 0 | 88 | 59 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | barrel Number
Take out the numerical order in the bucket and output the result: [62,14,16,88,59]
Again into the bucket, but this time the number of 10 digits, into the corresponding bucket, into the bottom of this: Because the front did a single digit of the order, so when the 10-digit number is equal, single-digit number is from small to large order into the bucket, that is, into the barrel or orderly
| 0 | 14,16 | 0 | 0 | 0 | 59 | 62 | 0 | 88 | 0 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | barrel Number
Because there is no greater than 100 of the number, there is no hundred digits, so to this sort of finished, in order to remove the
Final output: [14,16,59,62,88]
2. Example
Copy Code code as follows:
Cardinality sort C # Code
public static void Radixsort (int[] nums, int digit)
{
for (int k = 1; k <= Digit; k++)
{
int[] Tmparray = new int[nums. Length];
int[] Tmpcountingsortarray = new INT[10];
int i;
for (i = 0; i < Nums. Length; i++)
{
int tmpsplitdigit = nums[i]/(int) Math.pow (k-1)-(Nums[i]/(int) Math.pow (k)) * 10;
tmpcountingsortarray[tmpsplitdigit]++;
}
for (i = 1; i < tmpcountingsortarray.length; i++)
{
Tmpcountingsortarray[i] + = tmpcountingsortarray[i-1];
}
for (i = nums. Length-1; I >= 0; i--)
{
int tmpsplitdigit = nums[i]/(int) Math.pow (k-1)-(Nums[i]/(int) Math.pow (k)) * 10;
Tmparray[tmpcountingsortarray[tmpsplitdigit]-1] = nums[i];
tmpcountingsortarray[tmpsplitdigit]--;
}
for (i = 0; i < Nums. Length; i++)
{
Nums[i] = Tmparray[i];
}
}
}
int[] List = new[] {16, 14, 10, 8, 7, 9, 3, 2, 4, 1};
Sorter.radixsort (list, 2);