PHP implementation of radix sort
Radix sorting is based on the value of each bit in the keyword, and is sorted by performing several passes of "distribution" and "collection" on the sorted N elements.
Let’s use a specific example to show how radix sorting is performed.
Assume 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 expressed from 0 to 9.
So we might as well regard 0~9 as 10 buckets.
We first classify the sequence according to the single-digit numbers 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, the tens and hundreds digits can be sorted in this way, and finally the sorted sequence can be obtained.
<code><span><span><span><?php </span> /**基数排序**/</span><span>/* * 获取第几位上的数字 * *百位数 = 2345%1000/100 */</span><span><span>function</span><span>getN</span><span>(<span>$num</span>,<span>$N</span>)</span>{</span><span>$value</span> = <span>10</span>; <span>for</span>(<span>$i</span>=<span>1</span>;<span>$i</span>$N</span>;<span>$i</span>++){ <span>$value</span> = <span>$value</span> * <span>10</span>; } <span>$M</span> = (int)((<span>$num</span> % <span>$value</span> /(<span>$value</span>/<span>10</span>))); <span>return</span><span>$M</span>; } <span>/* */</span><span><span>function</span><span>paixu</span><span>(<span>$arr</span>)</span> {</span><span>$flag</span> = <span>1</span>;<span>//该次位数上是否全为0标志位,全为0 flag=0</span><span>for</span>(<span>$M</span>=<span>1</span>;<span>$flag</span>!=<span>0</span>;<span>$M</span>++) { <span>$flag</span> = <span>0</span>; <span>if</span>(<span>$M</span> > <span>1</span>){ <span>$m</span> = <span>0</span>; <span>for</span>(<span>$j</span>=<span>0</span>;<span>$j</span>10</span>;<span>$j</span>++){ <span>for</span>(<span>$k</span>=<span>0</span>;<span>$k</span><count>$b[<span>$j</span>]);<span>$k</span>++){ <span>if</span>(<span>$b</span>[<span>$j</span>][<span>$k</span>]!=<span>0</span>) <span>$arr</span>[<span>$m</span>++] = <span>$b</span>[<span>$j</span>][<span>$k</span>];<span>//将容器中的数按序取出,进行下一次排序</span> } } <span>$b</span> = <span>array</span>();<span>//再给b附新值前要清空数组中原有的数据</span> } <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><count>$arr);<span>$i</span>++) { <span>$thisNum</span> = getN(<span>$arr</span>[<span>$i</span>],<span>$M</span>); <span>if</span>(<span>$thisNum</span>!=<span>0</span>) <span>$flag</span> = <span>1</span>; <span>$b</span>[<span>$thisNum</span>][] = <span>$arr</span>[<span>$i</span>];<span>//将数组中的数放入容器中</span> } } print_r(<span>$arr</span>); <span>//var_dump($b);</span>} <span>/**基数排序**结束**/</span><span>?></span></count></count></code>
paixu(array(65,3,45,6,7,8,31,100,1000,1234))
The result is: Array ( [0] => 3 [1] => 6 [2] => 7 [3] => 8 [4] => 31 [5] => 45 [6] => 65 [7] => 100 [8] => 1000 [9] => 1234 )
Radix sorting can also be applied to find duplicate numbers, find interval numbers, etc.
The code is not important (my code still needs improvement), the idea is the key
The above introduces the PHP implementation of radix sorting, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How to use for to find n factorial: 1. Use the "for (var i=1;i<=n;i++){}" statement to control the loop traversal range to "1~n"; 2. In the loop body, use "cj *=i" Multiply the numbers from 1 to n, and assign the product to the variable cj; 3. After the loop ends, the value of the variable cj is the factorial of n, and then output it.

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.
