Home >Backend Development >PHP Tutorial >PHP implementation of radix sort

PHP implementation of radix sort

WBOY
WBOYOriginal
2016-07-29 08:59:54978browse

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.
PHP implementation of radix sort
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

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:break and continue in PHPNext article:break and continue in PHP