Home >Backend Development >PHP Tutorial >Solution to selection sorting using PHP_PHP tutorial

Solution to selection sorting using PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:43885browse

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

http: //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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn