Cardinal ranking in the "Big talk data structure" did not talk about, but in order to gather eight sorting algorithm, I learned this sort algorithm through the network, and to share it out. Basic idea:
The cardinality sort (radix sort) is an "distribution sort", also known as "bucket method" (bucket sort) or bin sort, which, as the name suggests, is the part of the key value that assigns the sorted element to some "buckets". In order to achieve the role of the order, the Cardinal order is a sort of stability, whose time complexity is O (Nlog (r) m), where R is the base to be taken, and M is the heap number, and in some cases the efficiency of the cardinality ranking method is higher than that of other stability ranking methods.
In fact, I can not sum up the idea, the following examples to illustrate it: the basic solution:
PS: Here we introduce the Cardinal order we use LSD (lowest priority), of course, there are MSD (top priority), we go to Baidu their own similarities and differences between it.
If we now have the following numbers:
2 343 342 1 128 43 4249 814 687 654 3
We use cardinality sorting to sort them from small to large.
The first step, based on the number of single-digit digits, is assigned to the bucket number 0 through 9 when the number of visits (from the front to the back is the same).
0:
1:1
2:2 342
3:343 3
4:814 654
5:
6:7:687 8:128
9:4,249
In the second step, the values in these buckets are then strung back up to form the following sequence:
1 2 342 343 43 3 814 654 687 128 4249
The third step, based on a 10-digit number, assigns them to the bucket numbered 0 through 9 when visiting the values (the previous and subsequent visits are the same):
0:1 2 3
1:814
2:128
3:
4:342 343 (4249
)
6:
7:
8:687
Step fourth, then reconnect the values in these buckets into the following sequence:
1 2 3 814 128 342 343 43 4249 654 687
The fifth step, based on the number of hundred digits, assigns them to the bucket numbered 0 through 9, after the number of visits (before and after the same step):
0:1 2 3
1:128
2:4249
3:342 343
4:
5:6:654 687
7:
8:814
Step sixth, then reconnect the values in these buckets into the following sequence:
1 2 3 43 128 4249 342 343 654 687 814
。。。。。。 The following steps should all be gone. In fact, at the sixth step, there are 4249 left.
From the above steps, many of the steps are the same, so it must be a cycle, we need to control a single, 10, hundreds of 、、、、 good.
Let's just look at the code. Algorithm implementation:
Swap functions function Swap (array & $arr, $a, $b) {$temp = $arr [$a];
$arr [$a] = $arr [$b];
$arr [$b] = $temp;
//Get the maximum number in the array//As in the example above, we end up with an algorithm that is just looking at the maximum value in the array: 4249, its number is the number of cycles the function Getmax (array $arr) {$max = 0;
$length = count ($arr);
for ($i = 0; $i < $length; $i + +) {if ($max < $arr [$i]) {$max = $arr [$i];
} return $max;
//Get the maximum number of digits, the number of digits that we allocate the bucket function getlooptimes ($maxNum) {$count = 1;
$temp = Floor ($maxNum/10);
while ($temp!= 0) {$count + +;
$temp = Floor ($temp/10);
return $count; /** * @param array $arr to be sorted array * @param $loop the number of cycles identified * The function is to complete only one (single or 10-bit) bucket sort/function R_sort (array & $arr, $l OOP) {//bucket array, in a strongly typed language, this array should be declared as [10][count ($arr)]///The first dimension is 0-9 10 numbers//The second dimension is so defined because it is possible that all the numbers in the array to be sorted are just the same, so they're all squeezed in one bucket.
Face $TEMPARR = Array ();
$count = count ($arr);
Initializes the $temparr array for ($i = 0; $i < $i + +) {$TEMPARR [$i] = array (); }//Bucket inDex's divisor//such as 798-bit barrel index= (798/1)%10=8/10-bit barrel index= (798/10)%10=9//Hundred Barrels index= (798/100)%10=7//$tempNum for 1, 10,
$tempNum = (int) POW ($loop-1);
for ($i = 0; $i < $count; $i + +) {//Find the number on a bit $row _index = ($arr [$i]/$tempNum)% 10; for ($j = 0; $j < $count; $j + +) {if (@ $tempArr [$row _index][$j] = NULL) {$TEMPARR [$row _index][ $J] = $arr [$i];
into the barrel break;
}///restore $K = 0 in the original array;
for ($i = 0; $i < $i + +) {for ($j = 0; $j < $count; $j + +) {if (@ $tempArr [$i] [$j]!= NULL) { $arr [$k + +] = $TEMPARR [$i] [$j]; The barrel $TEMPARR [$i] [$j] = NULL;
Avoid pollution data at the next cycle}}////final invocation of main function Radixsort (array & $arr) {$max = Getmax ($arr);
$loop = Getlooptimes ($max);
For each bucket allocation (1 for a bit, $loop represents the highest) for ($i = 1; $i <= $loop; $i + +) {R_sort ($arr, $i); }
}
Call algorithm:
$arr = Array (2, 343, 342, 1, 128, 4249, 814, 687, 654, 3);
Radixsort ($arr);
Var_dump ($arr);
In fact, the code I wrote before very early, today in the blog to find that, in fact, the bucket is a queue, so the above R_sort () function is complex, we use Array_push () and Array_shift () to rewrite the method (of course, to simulate the queue, with SPL The splqueue provided is the most appropriate, here for the sake of simplicity I do not have to:
function R_sort (array & $arr, $loop) {
$TEMPARR = array ();
$count = count ($arr);
for ($i = 0; $i < $i + +) {
$TEMPARR [$i] = array ();
}
The divisor of the index of the bucket
//such as 798-bit barrel index= (798/1)%10=8
//10-bit barrel index= (798/10)%10=9/
/Hundred Barrels index= (798/100)%10=7
/ /$tempNum for 1, 10,
$tempNum = (int) POW ($loop-1) in the upper-type;
for ($i = 0; $i < $count; $i + +) {
//Find the number on a bit
$row _index = ($arr [$i]/$tempNum)%;
Into barrel
array_push ($TEMPARR [$row _index], $arr [$i]);
}
Revert back to the original array
$k = 0;
for ($i = 0; $i < $i + +) {
//out of bucket while
(count ($tempArr [$i]) > 0) {
$arr [$k + +] = Array_shift ($TEMPARR [ $i]);}}
The Cardinal order is a sort of stability, and its time complexity is O (Nlog (r) m), where R is the base to be taken, and M is the heap number.
Well, here is the Cardinal order has been introduced to everyone. This algorithm is summarized mainly by looking at the information on the Internet, so no longer give the original author.