The disadvantage of counting and sorting is obvious. Additional space is required.CArray, although the time complexity isO (N + k)But when the element values in the input sequence are large, suchK = O (n2), The time complexity has reachedN2By an order of magnitude, the consumption of space is unacceptable. Here we will introduce another Linear sorting method.Algorithm-- Base sorting can cope with large numbers.
Base sorting is an algorithm that sorts a digit by digit. We often use in our daily life: to sort a date, the date is composed of years, months, and days. For this problem, we often use year Comparison first. If the year is the same and then the month is compared, and if it is the same, the day is compared.
similarly, we can compare a group of numbers. For example, we use this idea to sort the following four numbers: 123 , 312 , 245 , 531 , sort by hundred bits for the first time: 123 , 245 , 312 , 531 ; for the second time, sort by 10 digits: 312 , 123 , 531 , 245 ; the third sorting by single digit: 531 , 312 , 123 , 245 . Why? Why is the final result not as expected? The reason is that we started sorting from the high level, after almost the overall order, and then to the low level, all were disrupted. If we do this later, we will not be confused: The numbers with the same high positions will be sorted again. .... however, this implementation is more difficult.
Here, we sort the order from the lowest valid bit to the highest valid bit, so we still use the example above:
Single digit=>
10=>
Hundred bits
531312123
312123245
123
531
312
245245531
The result is correct. In layman's terms,The reason why the first row is low and then high is that the more digits are in the back row, the more impact it has on the result order. It is obvious that the higher order has a greater impact than the lower order!
Here is a simple proof process:
Assuming that a set of sequences are lowT-1The bits have been sorted. Now we followTBit. We only need to prove that for any two numbersTIts Relative position is correct after bit sorting. There are two conditions for any two numbers:
1)TheirTBITs are the same, so if we keep the stability of their positions, they are still ordered at last;
2)TheirTBit is different, according toTAfter the bits are sorted, they are sorted.
Therefore, this proves the correctness of the base sorting. Here we can see that the key point is that sorting must be stable! Soon I thought of using the Count sorting algorithm I mentioned earlier!
Then we can obtain the pseudo-count sortingCodeIs:
Radixsort (a, d) // D indicates the maximum number of digits in A. <br/> for I = 1 to d <br/> do use a stable sort to sort array A on digit I </P> <p>
The time complexity isO (D (n + k ));WhenDConstant,K = O (N)The base sorting has a linear running time.
More specific and more common algorithms:
/* n array to be sorted A Number of elements, B is a secondary array of saved data after sorting by bit count, C indicates the Count array, B indicates the maximum element length ( bit digits ), r is the length of each digit (that is, the count is sorted by 2R Base) */
Radixsort (int A [], int B [], int C [], int N, int B, int R) <br/> M = B/R; // m is the maximum number of digits <br/> for I = 0 M-1 DO <br/> for J = 0 to R-1 DO <br/> C [J] = 0; // The counting array is cleared. <br/> for J = 0 to n-1 DO <br/> C [(A [J]/t) % 2R] ++; // C [J] indicates the number of elements equal to j <br/> for j = 1 to R-1 DO <br/> C [J] = C [J] + C [J-1 ]; // C [J] indicates the number of elements equal to or less than j <br/> for J = n-1 downto 0 DO <br/> B [-- C [(A [J]/ t) % 2R] = A [J]; // count sorting <br/> for J = 0 to n-1 DO <br/> A [J] = B [J]; <br/> T = T * 2R; <br/>
Note: In the code, 2R refers to 2r!
Base sorting is suitable for sorting a number with a large value, and can also be used to sort strings.
However, the disadvantage of base sorting is that it does not show temporal and spatial locality, because the location of a number may change dramatically during the ranking of each number by bit, therefore, the advantages provided by modern machine cache cannot be fully utilized. When counting and sorting are stable in the middle, they do not have the characteristics of in-situ sorting. When the memory capacity is precious, it remains to be discussed.
Source code:
Http://download.csdn.net/source/3249610
References:
[1]Algorithm path.
Min hengming.Machinery Industry Press,2010.
[2]Introduction to Algorithms.
Cormen, T. H.Waiting for translation.Machinery Industry Press,2006.
Copyright statement: All Rights Reserved. Skip the review! Welcome to study and exchange. If any bug is found, please correct it!