Home > Backend Development > PHP Tutorial > php selection sort_PHP tutorial

php selection sort_PHP tutorial

WBOY
Release: 2016-07-13 17:53:47
Original
934 people have browsed it

[php]
//Select sort
//Sort from small to large


//date_default_timezone_set('Aisa/Shanghai');
$select=array();
for($i=0;$i<500;$i++)
{
$select[$i]=rand(0,3000);
}
function selectsort(&$arr)
{ 
$temp=0;
for($i=0;$i {
$minval=$arr[$i]; //Every time, the i-th number is considered to be the minimum value
$minindex=$i;
for($j=$i+1;$j                                                                            //Indicates that the current value is not the minimum value
If($minval>$arr[$j])
                                                                                             $minval=$arr[$j];
                  $minindex=$j;
                                                                                                                                                                                                            //Exchange is performed after the inner for loop ends. This is where selection sorting, called bubble sorting, is superior.
            $temp=$arr[$i];
            $arr[$i]=$arr[$minindex];
             $arr[$minindex]=$temp;
}  

}
​ selectsort($select);
Print_r($select);
//date_default_timezone_set('Aisa/Shanghai');
?>
[php]




http://www.bkjia.com/PHPjc/477996.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/477996.htmlTechArticle[php] ?php //Select sort//Sort from small to large//date_default_timezone_set(Aisa/Shanghai); $select=array(); for($i=0;$i500;$i++) { $select[$i]=rand(0,3000); } function selectsort($a...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template