PHP Learning Note 12 "array sorting"

Source: Internet
Author: User

<?PHP$arr=Array(0,5,-1); $temp=0;  for($i= 0;$i<Count($arr)-1;$i++) {     for($j= 0;$j<Count($arr)-1-$i;$j++)    {       if($arr[$j]>$arr[$j+1])       {          $temp=$arr[$j]; $arr[$j]=$arr[$j+1]; $arr[$j+1]=$temp; }    }  } foreach($arr  as $val) {     Echo"<br>".$val; } Selectsort ($arr); Print_r($arr); Insertsort ($arr); Print_r($arr); //Select Sort functionSelectsort (&$arr) {         $temp=0;  for($i= 0;$i<=Count($arr)-1;$i++)         {             //Let's say $i is the smallest number.             $minval=$arr[$i]; //Record the subscript of the minimum number I think             $minIndex=$i;  for($j=$i+1;$j<Count($arr);$j++)             {                //describe the minimum value we think, not the smallest                if($minval>$arr[$j])                {                  $minval=$arr[$j]; $minIndex=$j; }                //the last Exchange                $temp=$arr[$i]; $arr[$i]=$arr[$minIndex]; $arr[$minIndex]=$temp; }                      } }   //Insert Sort  functionInsertsort (&$arr)  {     //The default subscript is 0 This number is ordered       for($i= 0;$i<Count($arr);$i++)      {      //$insertVal is the number that is ready to be inserted         $insertVal=$arr[$i]; //prepare to compare with $insertindex first .         $inserIndex=$i-1; //If this condition is met, it is stated that we have not found a suitable location          while($inserIndex>0&&$insertVal<$arr[$inserIndex])         {           //and move the number back           $arr[$inserIndex+1]=$arr[$inserIndex]; $inserIndex--; }         //Insert (then give $insertval the appropriate location)         $arr[$inserIndex+1]=$insertVal; }  }?>

PHP Learning Note 12 "array sorting"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.