首頁 > 後端開發 > php教程 > php 冒泡排序

php 冒泡排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2016-07-29 09:05:41
原創
858 人瀏覽過

<code><span>/**
 * 冒泡排序
 *<span> @global</span> obj $db
 *<span> @param</span> array  排序数組
 *<span> @param</span> value  按照此字段的大小排序
 */</span><span><span>function</span><span>bubble_sort</span><span>(<span>$array</span>, <span>$value</span>)</span> {</span><span>$array</span> = array_values(<span>$array</span>);  <span>//将数据键值按照数字重新排序</span><span>$count</span> = count(<span>$array</span>);

    <span>if</span>(<span>$count</span> <= <span>0</span> || <span>empty</span>(<span>$value</span>)) <span>continue</span>;
    <span>for</span> (<span>$i</span>=<span>0</span>; <span>$i</span> < <span>$count</span> ; <span>$i</span>++) { 
        <span>for</span> (<span>$m</span>=<span>$count</span>-<span>1</span>; <span>$m</span> > <span>$i</span>; <span>$m</span>--) { 
            <span>if</span>(<span>$array</span>[<span>$m</span>][<span>$value</span>] < <span>$array</span>[<span>$m</span> -<span>1</span>][<span>$value</span>]){
                <span>$tmp</span> = <span>$array</span>[<span>$m</span>];
                <span>$array</span>[<span>$m</span>] = <span>$array</span>[<span>$m</span>-<span>1</span>];
                <span>$array</span>[<span>$m</span>-<span>1</span>] = <span>$tmp</span>;
            }
        }
    }
    <span>return</span><span>$array</span>;
}</code>
登入後複製

<code><span>$array</span> = <span>array</span>(
    <span>'5'</span>=><span>array</span>(<span>'a'</span>=><span>'a'</span>,<span>'b'</span>=><span>'b'</span>,<span>'value'</span>=><span>''</span>),
    <span>'6'</span>=><span>array</span>(<span>'b'</span>=><span>'c'</span>,<span>'d'</span>=><span>'d'</span>,<span>'value'</span>=><span>4</span>),
    <span>'3'</span>=><span>array</span>(<span>'value'</span>=><span>3</span>),
    <span>'4'</span>=><span>array</span>(<span>'value'</span>=><span>2</span>)
);

<span>$m</span> = bubble_sort(<span>$array</span>, <span>'value'</span>);
print_r(<span>$m</span>);</code>
登入後複製

列印輸出
Array ( [0] => Array ( [a] => a [b] => b [value] => )
[1] => Array ( [value] => 2 )
[2] => Array ( [value] => 3 )
[3] => Array ( [b] => c [d] => d [value] => 4 ) )

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

以上就介紹了php 冒泡排序,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
數組轉化array
來自於 1970-01-01 08:00:00
0
0
0
老師,為什麼我輸出的Array是空的
來自於 1970-01-01 08:00:00
0
0
0
es6 - JavaScript如何將string轉array
來自於 1970-01-01 08:00:00
0
0
0
把兩個Array合併為一個json的格式
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板