Home >Backend Development >PHP Tutorial >PHP implements bubble algorithm_PHP tutorial
<?<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>