Home  >  Article  >  Backend Development  >  PHP implements bubble algorithm_PHP tutorial

PHP implements bubble algorithm_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:51862browse

<?<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 /" Time complexity: O(n^2)...
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