Home >Backend Development >PHP Tutorial >php insertion sort_PHP tutorial
[php]
//Insertion sort Sort from small to large
$insert=array();
for($i=0;$i<200;$i++)
{
$insert[$i]=rand(0,30000);
}
//print_r($insert);
function insertsort(&$arr)
{
//Insertion sort treats the first one as ordered, so i starts from 1
for($i=1;$i
$insertindex=$i-1;
//Find the insertion point
while($insertindex>=0&&$insertval<$arr[$insertindex])
{ //Move the number backward
$arr[$insertindex+1]=$arr[$insertindex];
$insertindex--;
//Insert value
$arr[$insertindex+1]=$insertval;
}
}
insertsort($insert);
print_r($insert);
?>
http://www.bkjia.com/PHPjc/477995.html