The base sort of the PHP sorting algorithm (Radix sort

Source: Internet
Author: User
This article mainly introduces the PHP sorting algorithm of the base order (Radix sort), combined with the case of a detailed analysis of the PHP base sorting algorithm principles, implementation methods and related use skills, the need for friends can refer to the next

The example in this paper describes the base ordering of the PHP sorting algorithm (Radix sort). Share to everyone for your reference, as follows:

The radix sort is not mentioned in the "Big Talk data Structure", but in order to gather eight sorts of algorithms, I have learned this sort algorithm through the network, and give everybody to share out.

Basic idea:

The radix sort (radix sort) belongs to the "distributive sort" (distribution sort), also known as the "bucket method" (bucket sort) or bin sort, as the name implies, it is a part of the information through the key value, the elements to be sorted into some "barrels", In order to achieve the role of sequencing, the cardinal ranking method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the base taken, and M is the number of heaps, at some point, the cardinality of the sorting method is more efficient than other stability ranking method.

In fact, I can not summarize the idea, the following examples to illustrate it:

Basic solution:

PS: Here we introduce the radix sort we use LSD (lowest bit priority), of course, and MSD (top priority), we go to Baidu for their similarities and differences between them.

If we now have the following numbers:

2 343 342 1 128 43 4249 814 687 654 3

We use the cardinality sort to sort them from small to large.

The first step, first, based on the number of digits, the number of visits (from front to back, the following steps are the same) when they are assigned to the buckets numbered 0 to 9:

0:
1:1
2:2 342
3:343 43 3
4:814 654
3 |
6:
7:687
8:128
9:4,249

In the second step, the values in these buckets are then re-threaded to become the following sequence:

1 2 342 343 43 3 814 654 687 128 4249

The third step, based on the 10-digit numeric value, assigns them to the buckets numbered 0 through 9 when visiting values (from front to back, same as later steps):

0:1 2 3
1:814
2:128
5 |
4:342 343 43 4249
5:654
6:
7:
8:687
9:

In the fourth step, the values in these buckets are then re-threaded to become the following sequence:

1 2 3 814 128 342 343 43 4249 654 687

The fifth step, according to the number of the hundred, in the number of visits (from front to back, the same as the next step), they are assigned to the buckets numbered 0 to 9:

0:1 2 3 43
1:128
2:4,249
3:342 343
4:
3 |
6:654 687
7:
8:814
9:

In the sixth step, the values in these buckets are then re-threaded to become the following sequence:

1 2 3 43 128 4249 342 343 654 687 814

。。。。。。 The next steps are all going to be gone. In fact, in the sixth step when the remaining 4249 is not a good order.

From the above steps, a lot of the steps are the same, so it must be a loop, we only need to control the bit, 10, hundreds 、、、、.

Let's look at the code.

Algorithm implementation:

Swap functions function Swap (array & $arr, $a, $b) {$temp = $arr [$a];  $arr [$a] = $arr [$b]; $arr [$b] = $temp;}  Gets the maximum number in the array//As in the example above, we will eventually stop the algorithm but look at the maximum value in the array: 4249, the number of bits is the number of cycles function Getmax (array $arr) {$max = 0;  $length = count ($arr);    for ($i = 0; $i < $length; $i + +) {if ($max < $arr [$i]) {$max = $arr [$i]; }} return $max;}  Gets the maximum number of bits, the maximum number of bits is the number of times we allocate a bucket function getlooptimes ($maxNum) {$count = 1;  $temp = Floor ($maxNum/10);    while ($temp! = 0) {$count + +;  $temp = Floor ($temp/10); } return $count;} /** * @param array $arr array to sort * @param $loop the number of times the loop identifies * The function just completes the bucket sort on one (single or 10 bit) */function R_sort (Array & $arr, $loop) {/ /bucket array, in a strongly typed language, this array should be declared as [10][count ($arr)]//The first dimension is 0-9 10 number//The second dimension is defined because it is possible to sort all the numbers in the array with just the same on one, so it is all squeezed in a bucket $TEMPARR =  Array ();  $count = count ($arr);  Initialize the $temparr array for ($i = 0; $i < $i + +) {$TEMPARR [$i] = array (); }//Find the divisor of the index of the bucket//such as 798 barrels index= (798/1)%10=8//10-bit barrels 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 + +) {//To 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];      Break into barrels;  }}}//revert back to the original array $k = 0; 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 polluting the data at the next cycle}}}}//the final call to the main function Radixsort (array & $arr) {$max = Getmax ($arr);  $loop = Getlooptimes ($max);  For each one, the bucket allocation (1 for digit, $loop for the highest) for ($i = 1; $i <= $loop; $i + +) {R_sort ($arr, $i); }}

Call algorithm:

$arr = Array (2, 343, 342, 1, 128, 43, 4249, 814, 687, 654, 3); Radixsort ($arr); Var_dump ($arr);

Operation Result:

Array (one) {[0]=> int (1) [1]=> int (2) [2]=> int (3) [3]=> int (+) [4]=> int (+) [5]=> int (342) [6]=> Int (343) [7]=> Int (654) [8]=> Int (687) [9]=> int (814) [10]=> int (4249)}

In fact, the code I wrote earlier, 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 is the splqueue most appropriate, I don't need to be here for the sake of simplicity):

function R_sort (array & $arr, $loop) {  $TEMPARR = array ();  $count = count ($arr);  for ($i = 0; $i < $i + +) {    $TEMPARR [$i] = array ();  }  Find the divisor of the index of the bucket  //such as 798 barrels index= (798/1)%10=8  //10-bit barrels index= (798/10)%10=9  //Hundred Barrels index= (798/100)%10=7  / /$tempNum is 1, 10,  $tempNum = (int) POW (1) in the above formula;  for ($i = 0; $i < $count; $i + +) {    //To find the number on a bit    $row _index = ($arr [$i]/$tempNum)%;    Into    the barrel array_push ($TEMPARR [$row _index], $arr [$i]);  }  Revert back to the original array  $k = 0;  for ($i = 0; $i < $i + +) {    //out bucket while    (count ($tempArr [$i]) > 0) {      $arr [$k + +] = Array_shift ($TEMPARR [ $i]);}}}  

The cardinality ordering method is a sort of stability, with a time complexity of O (Nlog (r) m), where R is the base taken, and M is the number of heaps.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.