1. Overview
Base sorting (Radix sort) is a non-Comparative integer sorting algorithm. The principle is to cut an integer into different digits by the number of digits and then compare them by the number of digits. Since integers can also express strings (such as names or dates) and floating point numbers in specific formats, the base sorting is not only applicable to integers. The invention of the base sort can be traced back to the contribution of Herman he Leili in the Tabulation Machine in 1887.
Principle: unify all the values to be compared (positive integers) into the same digit length, and add zeros before the shorter digits. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit. The time complexity of base sorting is O (k · n), where n is the number of sorting elements and k is the number of digits.
Understanding: similar to the [classic algorithm], the eighth time is bucket sorting. 10 buckets are always needed here. It is used multiple times. First, the bucket is loaded with single-digit values, that is to say, if the single digit is 1, it is placed in the bucket 1. If the single digit is 9, it is placed in the bucket 9. Then, it is sorted by ten digits, and so on.
If there are five numbers in the array to be sorted: [, 14, 59,88, 16], 10 buckets are allocated, the bucket number is 0-9, and the bucket number is entered in sequence with a single-digit number, to the bottom.
| 0 | 0 | 62 | 0 | 14 | 0 | 16 | 0 | 88 | 59 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | bucket ID
Obtain the numbers in the bucket in sequence. The output result is [, 14, 59].
Enter the bucket again. However, this time, the bucket is entered based on the ten-digit number. The following is the case: When the ten-digit number is equal, the number of a single digit is in the ascending order of the bucket, that is, whether the bucket is in the ascending order.
| 0 | 14,16 | 0 | 0 | 0 | 59 | 62 | 0 | 88 | 0 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | bucket ID
Because there are no numbers greater than 100 and no hundred digits, the sorting is complete and the order can be taken out.
Final output result: [,]
2. Example
Copy codeThe Code is as follows:
// Sort the C # Code by the Base.
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 (10, k-1)-(nums [I]/(int) Math. pow (10, 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 (10, k-1)-(nums [I]/(int) Math. pow (10, 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 );