$arr = [9, 43, 12, 0, 87, 1]; function shell_sort(&$arr){ $gap = count($arr) -1; $max = count($arr) -1; do{ $gap = $gap/2; for($i = 0; $i <= $max - $gap; $i++){ for($j = $i; $j <= $max - $gap; $j += $gap){ if($arr[$j] < $arr[$j + $gap]){ swap($arr, $j, $j + $gap); } } } }while($gap > 1); } shell_sort($arr); var_export($arr);
时间: 2024-10-21 23:40:21