Skip to content

Instantly share code, notes, and snippets.

@Jason-cqtan
Created September 17, 2019 04:26
Show Gist options
  • Save Jason-cqtan/62ec8e8653cf9ce658672e9f8d29b38f to your computer and use it in GitHub Desktop.
Save Jason-cqtan/62ec8e8653cf9ce658672e9f8d29b38f to your computer and use it in GitHub Desktop.
php选择排序
function findSmall($arr){
$small = $arr[0];
$small_index = 0;
for ($i=0; $i < count($arr); $i++) {
if($arr[$i] < $small){
$small_index = $i;
$small = $arr[$i];
}
}
return $small_index;
}
function selectSort($arr)
{
$newArr = [];
$lool = count($arr);
for ($i=0; $i < $lool; $i++) {
$small_index = findSmall($arr);
// print_r(array_splice($arr,$small_index,1));
$newArr[] = $arr[$small_index];
unset($arr[$small_index]);
$arr = array_values($arr);
}
return $newArr;
}
print_r(selectSort([1,23,1212,0,-1]));
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment