选择排序
/**
* 选择排序
*/
function findSmallest($list)
{
$small = $list[0];
$k = 0;
foreach ($list as $key => $value) {
if ($value <= $small) {
$small = $value;
$k = $key;
}
}
return $k;
}
function selectionSort($list)
{
$arr = [];
$num = count($list);
for ($i = 0; $i < $num; $i++) {
$smallestKey = findSmallest($list);
$arr[] = $list[$smallestKey];
unset($list[$smallestKey]);
$list = array_values($list);
}
return $arr;
}

浙公网安备 33010602011771号