Home >Backend Development >PHP Tutorial >PHP algorithm selection sort
The content introduced in this article is the selection sorting code in the PHP algorithm. Now I share it with you. Friends in need can also refer to it. Let’s take a look together.
<?php // //选择排序 function SelectionSort($arr) { for($i = 0;$i<count($arr);$i++){ $min = $i; for($j = $i;$j<count($arr);$j++){ if($arr[$min]>$arr[$j]){ $min = $j; } } if($min != $i){ $temp = $arr[$min]; $arr[$min] = $arr[$i]; $arr[$i] = $temp; } } return $arr; } $arr = array(2,5,8,4,3,1,6,7,9); $old_array = SelectionSort($arr); print_r($old_array);exit;
Related recommendations:
The above is the detailed content of PHP algorithm selection sort. For more information, please follow other related articles on the PHP Chinese website!