PHP实现冒泡算法_PHP教程

WBOY
發布: 2016-07-13 10:35:51
原創
863 人瀏覽過

<?<span php

</span><span //</span><span php函数:count($arr)返回array的数值总数. </span>
     <span function</span> bubble_sort(<span $arr</span><span ){
             </span><span for</span> (<span $i</span> =  6;<span $i</span> > 0;<span $i</span> --<span ){
                   </span><span for</span> (<span $j</span> = 0;<span $j</span> < <span $i</span>; <span $j</span> ++<span ){
                         </span><span if</span> (<span $arr</span>[<span $j</span>] < <span $arr</span>[<span $j</span> + 1<span ]){
                              </span><span $tem</span> = <span $arr</span>[<span $j</span><span ];
                              </span><span $arr</span>[<span $j</span>] = <span $arr</span>[<span $j</span> + 1<span ];
                              </span><span $arr</span>[<span $j</span> + 1] = <span $tem</span><span ;
                        }
                  }
            }
             </span><span foreach</span> (<span $arr</span> <span as</span> <span $rows</span><span ){
                   </span><span echo</span> <span $rows</span>."<br />"<span  ;
            }
      }


</span><span //</span><span 使用两层循环,外层控制循环的次数,内层控制遍历数组内数值<br /><br />时间复杂度:O(n^2)<br /><br /></span>
登入後複製

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/742173.htmlTechArticle? bubble_sort( ( = 6; 0; -- ( = 0; ; ++ ( [ ] [ + 1 = [ [ ] = [ + 1 [ + 1] = ( ."br /" 时间复杂度:O(n^2)...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!