1. Definition: Selection sort is a simple and intuitive sorting algorithm. Here's how it works. First, find the smallest (large) element in the unsorted sequence, and store to the starting position of the sorted sequence . Then, continue to find the smallest (large) element from the remaining unsorted elements, and then put it in the sorted sequence. end of sequence. And so on until all elements are sorted.
Reference code:
Copy code The code is as follows:
//Selection sort is a simple and intuitive sorting algorithm. Here's how it works. First, find the smallest (large) element in the unsorted sequence and store it at the beginning of the sorted sequence. Then, continue to find the smallest (large) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on until all elements are sorted.
function selectSort(&$arr){
//Define variables for exchange
$temp=0;
for($i=0;$i
;
for($j=$i+1;$j //Exchange if the minimum value is greater than the following number
if($valmin>$ Arr [$ j]) {
$ valmin = $ arr [$ j];
$ minKey = $ j;
}
}
// $arr[$i];
$arr[$i]=$arr[$minkey];
$arr[$minkey]=$temp;
}
}
$arr=array(7,5,0,4,-1);
selectSort($arr);
print_r($arr);
?>
http://www.bkjia.com/PHPjc/327044.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/327044.htmlTechArticle1. Definition: Selection sort is a simple and intuitive sorting algorithm. Here's how it works. First find the smallest (large) element in the unsorted sequence and store it in the sorted order...