This article mainly introduces the method of implementing radix sorting in PHP, and analyzes the principles, implementation methods and related operating techniques of radix sorting in the form of examples. Friends in need can refer to the following
The examples in this article describe PHP How to implement radix sort. Share it with everyone for your reference, the details are as follows:
Cardinal sorting is based on the value of each bit in the keyword, and the sorting is achieved by performing several passes of "distribution" and "collection" on the sorted N elements.
Want to use a specific example to show how radix sorting is performed.
Suppose an initial sequence is: R {50, 123, 543, 187, 49, 30, 0, 2, 11, 100}.
We know that for any Arabic number, the base of each digit is represented by 0~9.
So we might as well regard 0~9 as 10 buckets.
We first classify according to the single-digit numbers of the sequence and divide them into specified buckets. For example: R[0] = 50, the single digit is 0, store this number in the bucket numbered 0.
After classification, we take out all the numbers from each bucket in order from number 0 to number 9.
At this time, the obtained sequence is a sequence with an increasing trend in single digits.
Sort by single digits: {50, 30, 0, 100, 11, 2, 123, 543, 187, 49}.
Next, you can sort the tens and hundreds digits in this way, and finally you can get the sorted sequence.
1){ $m = 0; for($j=0;$j<10;$j++){ for($k=0;$k
Run result:
Copy codeThe code is as follows:
Array ( [0] => 3 [1] => 6 [2] => 7 [3] => 8 [4] => 31 [5] => 45 [6 ] => 65 [7] => 100 [8] => 1000 [9] => 1234 )
PS: Here is another article about Sorting demonstration tools for your reference:
Online animation demonstration of insertion/selection/bubble/merge/Hill/quick sort algorithm process tools:
http://tools.jb51.net/aideddesign/paixu_ys
PHP is based on reflection Explanation of the method of realizing automatic dependency injection by mechanism
The above is the detailed content of An explanation of how to implement radix sorting in PHP. For more information, please follow other related articles on the PHP Chinese website!