首页 >后端开发 >php教程 >PHP实现冒泡算法_PHP教程

PHP实现冒泡算法_PHP教程

WBOY
WBOY原创
2016-07-13 10:35:51888浏览

<?<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)...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn