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

php insertion sort_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:53:49999browse

[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                                                                              $insertval=$arr[$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

truehttp: //www.bkjia.com/PHPjc/477995.htmlTechArticle[php] ?php //Insertion sort is sorted from small to large $insert=array(); for($ i=0;$i200;$i++) { $insert[$i]=rand(0,30000); } //print_r($insert); function insertsort($arr) { //Insertion sort...
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