?php$b = [5,2,7,3,1,6,8,0];// $b = [8,7,6,5,4,3,2,1,0];#直观地看出需要复循环几遍$n = count($b); for($s = 0;$s $n;$s++){ for($i = 0;$i = $n;$i++){ if (!isset($b[$i+1])) break;# 防止偏移溢出 if ($b[$i]$b[$i+1]) { #升序排序
<?php $b = [5,2,7,3,1,6,8,0]; // $b = [8,7,6,5,4,3,2,1,0];#直观地看出需要复循环几遍 $n = count($b); for($s = 0;$s < $n;$s++){ for($i = 0;$i <= $n;$i++){ if (!isset($b[$i+1])) break;# 防止偏移溢出 if ($b[$i]>$b[$i+1]) { #升序排序 $tmp = $b[$i+1]; $b[$i+1] = $b[$i]; $b[$i] = $tmp; } } } print_r($b);