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

PHP实现冒泡算法_PHP教程

WBOY
发布: 2016-07-13 10:35:51
原创
902 人浏览过

<?<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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板