Created
September 17, 2019 04:26
-
-
Save Jason-cqtan/62ec8e8653cf9ce658672e9f8d29b38f to your computer and use it in GitHub Desktop.
php选择排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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