$nums = [4, 5, 6, 3, 2, 1]; for($i=0;$i<count($nums);$i++){ $min = $i; for($j=$i+1;$j<count($nums);$j++){ if($nums[$j] < $nums[$min]){ $min = $j; } } if($min != $i){ $tmp = $nums[$i]; $nums[$i]=$nums[$min]; $nums[$min]=$tmp; } } print_r($nums);
$nums = [4, 5, 6, 3, 2, 1]; for($i=0;$i<count($nums);$i++){ $min = $i; for($j=$i+1;$j<count($nums);$j++){ if($nums[$j] < $nums[$min]){ $min = $j; } } if($min != $i){ $tmp = $nums[$i]; $nums[$i]=$nums[$min]; $nums[$min]=$tmp; } } print_r($nums);