Preface
Base sorting (Radix sort) is also called bucket sort. Compared with common comparative sorting, base sorting is a distributed sorting, that is, the process of sorting is completed by assigning all the numbers to the correct position and then overwriting the original array. The counting sorting I mentioned in the previous article also belongs to this sort mode. The last article mentioned the stability of counting sorting, that is, the relative positions of the same numbers before and after sorting remain unchanged. The base sorting we want to talk about today is to use the stability of sorting.
Thinking process
Let's recall how we learned to compare numbers when we were young? We first compare the number of digits. If one digit is more than the other, the number must be larger. If the number of digits is the same, the comparison will be performed in descending order. If the number of digits is larger, the comparison will be stopped, the conclusion is that the number in this position is larger. Of course, we can also start from the smallest bit, which corresponds to the MSD (most significant digital) and least significant digital in the base sort.
After thinking about this, we need to consider how to store the sorting results of each bit. First, as a distributed sorting, Lenovo counts sorting, the data structure of the sorting result stored in each bit of sorting should be at least a 10-length array (corresponding to the decimal digit 0-9 ). At the same time, the following situations may occur: the numbers of all elements in the original array on this bit are the same, and the one-dimensional array cannot meet our needs, we need a two-dimensional array of 10 * n (n is the length of the array) to store the sorting results of each bit. Readers who are familiar with counting sorting results may be curious: Why can't they store only the number of times this number appears at each position, just like counting sorting, without storing specific values, so we can use a one-dimensional array? Let's take a look at this question first after analyzing the base sort.
Now we can store the result of each bit sorting. To use the result of this sorting before the next sorting, we need to restore the result of sorting in the bucket to the original array, then, we continue to sort the modified original array by bit. In this loop, the final result is that the elements in the array are first sorted by the highest bit, and the elements in the array are sorted by the same highest bit, recurrence. Returns an array of sorted results.
Algorithm process
- Initialization:Construct a 10 * n two-dimensional array with a length of N to store the number of elements in each bucket during each bit sorting.
- Loop operation:Starting from the low position (we use LSD), we store all the elements in the corresponding bucket (corresponding to the column of the Two-dimensional array ). Then, all the elements in the bucket are taken from small to large according to the sub-label of the bucket. For elements in the Same bucket, first put them in and take them out, after putting it in (to ensure the sorting stability ). In this way, the original array is sorted by this bit, And the next bit operation continues until the highest bit sorting is completed.
Here is an example to help you understand:
We have an array: 73, 22, 93, 43, 55, 14, 28, 65, 39, 81
The following is the sorting process (each column in a two-dimensional array corresponds to a bucket, because the bucket space is not used up, so the two-dimensional array is not fully painted ):
1. sort by bit
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|
|
81 |
22 |
73 |
14 |
55 |
|
|
28 |
39 |
|
|
|
|
93 |
|
65 |
|
|
|
|
|
|
|
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Array result after sorting by the first digit:
, 22
We can see that the array has been sorted by bit.
2. Sort results by hundreds of digits
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|
|
14 |
22 |
39 |
43 |
55 |
65 |
73 |
81 |
93 |
|
|
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Retrieve the sorting result:
We can see that on the basis of the single-digit sorting, the hundred-bit sorting is also completed (for hundreds of identical numbers, such as, because the single-digit sorting is completed, the sorting stability is maintained during retrieval, therefore, the positions of these two numbers are determined based on their single-digit sorting results ). Because the original array has a maximum of hundreds of elements, the original array completes the sorting process.
Summary
Now let's take a look at the two issues left over: Why can't we use a one-dimensional array? We must use a two-dimensional array structure similar to a bucket to store the intermediate bit sorting results? In fact, I want to write this question because I think this question is the key to understanding the base sorting. The principle of base sorting is very simple, but there are two problems to be considered in implementation: 1. How to retain the sorting result of the previous bit can be solved by the sorting stability mentioned earlier. 2. How to associate the bit sorting result with the original array element. A two-dimensional array is used to solve this problem. In counting sorting, although the relative positions of all equal elements are retained, there is no difference in counting sorting, therefore, we can only save the number of such elements in the array. While in the base sorting, some elements are the same on one bit, but they may be different on the other bit. If you only save the number of 5 or 6 on the bit, the information about other elements is discarded, so that they cannot be sorted at a higher level.
After figuring out the base sorting process, let's look at the time complexity of this algorithm? In each loop traversal array, the elements are placed in the specified position round (n), the data round (n) is retrieved from the bucket, and the loop is d times (D is the number of digits ), time complexity is round (R * n)
The following is a Java implementation of base sorting:
Package sort; public class radixsort {Private Static void radixsort (INT [] array, int d) {int n = 1; // represents the number of digits: 100... int K = 0; // Save the result of each sorting bit for the next sorting input int length = array. length; int [] [] bucket = new int [10] [length]; // The sorting bucket is used to save the result after each sorting, place the numbers with the same sorting result in the same bucket in int [] Order = new int [length]; // used to save the number of while (n <D) {for (INT num: array) in each bucket) // place each number in the array in the corresponding bucket {int digit = (Num/n) % 10; bucket [digit] [order [digit] = Num; Order [digit] ++;} For (INT I = 0; I <length; I ++) // overwrite the data in the bucket generated by the previous loop to the original array to save the sorting result {If (Order [I]! = 0) // The bucket contains data. traverse the bucket from top to bottom and save the data to the original array {for (Int J = 0; j <order [I]; j ++) {array [k] = bucket [I] [J]; k ++ ;}} order [I] = 0; // set the counter in the bucket to 0, used for next bit sorting} n * = 10; k = 0; // set K to 0, used to save the sorting result of the next round} public static void main (string [] ARGs) {int [] A = new int [] {, 22, 93, 43, 55, 14, 28, 65, 39, 81}; radixsort (A, 100); For (INT num: a) {system. out. println (Num );}}}
The program running result is as follows: