Home >Backend Development >PHP Tutorial >php selection sort_PHP tutorial
[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
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