Home  >  Article  >  Backend Development  >  PHP bubble sort

PHP bubble sort

WBOY
WBOYOriginal
2016-08-08 09:21:41833browse

When I went to Kugou for an interview yesterday, in the written test, there was a programming question that asked me to use PHP to implement bubble sorting. Because I haven’t used bubble sorting for a long time, I forgot the principle of the algorithm, and the result was left blank. , really speechless. So today I will record the PHP bubble sorting code:

<code><span><?php</span><span>/**
 * 冒泡排序
 *<span> @param</span> array $numbers 要排序的数组,只限数字一维数组
 *<span> @param</span> boolean $asc   排序顺序,true是正序,false是逆序
 */</span><span><span>function</span><span>bubble_sort</span><span>(array <span>$numbers</span>, <span>$asc</span> = true)</span> {</span><span>$n</span> = count(<span>$numbers</span>);
    <span>// 外循环最多排(n - 1)次</span><span>$out_loop_cnt</span> = <span>$n</span> - <span>1</span>;
    <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> < <span>$out_loop_cnt</span>; <span>$i</span>++) {
        <span>// 内循环最多排(n - i - 1)次</span><span>$in_loop_cnt</span> = <span>$n</span> - <span>$i</span> - <span>1</span>;
        <span>for</span> (<span>$j</span> = <span>0</span>; <span>$j</span> < <span>$in_loop_cnt</span>; <span>$j</span>++) {
            <span>// 根据排序顺序判断相邻两个数是否符合交换条件</span><span>$swap</span> = <span>$asc</span> ? (<span>$numbers</span>[<span>$j</span>] > <span>$numbers</span>[<span>$j</span> + <span>1</span>])
                : (<span>$numbers</span>[<span>$j</span>] < <span>$numbers</span>[<span>$j</span> + <span>1</span>]);
            <span>if</span> (<span>$swap</span>) {
                <span>$temp</span> = <span>$numbers</span>[<span>$j</span> + <span>1</span>];
                <span>$numbers</span>[<span>$j</span> + <span>1</span>] = <span>$numbers</span>[<span>$j</span>];
                <span>$numbers</span>[<span>$j</span>] = <span>$temp</span>;
            }
        }
    }

    <span>return</span><span>$numbers</span>;
}

<span>$arr</span> = [<span>1</span>, <span>3</span>, <span>5</span>, <span>8</span>, <span>4</span>];

var_dump(bubble_sort(<span>$arr</span>));
<span>/**
 * 输出:
 * array (size=5)
 * 0 => int 1
 * 1 => int 3
 * 2 => int 4
 * 3 => int 5
 * 4 => int 8
 */</span>var_dump(bubble_sort(<span>$arr</span>, <span>false</span>));
<span>/**
 * 输出:
 * array (size=5)
 * 0 => int 8
 * 1 => int 5
 * 2 => int 4
 * 3 => int 3
 * 4 => int 1
 */</span></code>

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced bubble sorting in PHP, 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