选择排序
选择排序
时间复杂度O(n^2)
//选择排序
function selectionSort(&$arr)
{
$len = count($arr);
if ($len <= 1) return;
for ($i=0;$i<$len-1;$i++) {
$min_index = $i;
for ($j=$i+1;$j<$len;$j++) {
if ($arr[$j] < $arr[$min_index]) {
$min_index = $j;
}
}
if ($min_index != $i) {
$temp = $arr[$i];
$arr[$i] = $arr[$min_index];
$arr[$min_index] = $temp;
}
}
}
$arr = [1,4,6,2,3,5,4];
selectionSort($arr);
print_r($arr);