php--bubble, select, insert Sort method

Source: Internet
Author: User

Use PHP to implement three of common sorting methods:

1<?PHP2 /**3 * Created by Phpstorm.4 * User:xxx5 * DATE:2016/10/126 * time:21:387  */8 //Bubble Sort Method9 functionMaopao_sort (&$arr){Ten     //External loop control count ($arr)-1 times to complete sorting One      for($i= 0;$i<Count($arr)-1;$i++){ A         //internal loop control every trip, find the biggest number: Ccount ($arr)-$i -          for($j= 0;$j<Count($arr)-1-$i;$j++){ -             if($arr[$j] >$arr[$j+1]){ the                 $temp=$arr[$j+1]; -                 $arr[$j+1] =$arr[$j]; -                 $arr[$j] =$temp; -             } +         } -     } + } A  at //Select Sort Method - functionSelectsort (&$arr){ -      for($i= 0;$i<Count($arr)-1;$i++){ -         //minimum number of assumptions -         $minVal=$arr[$i]; -         //Subscript of the minimum number in         $minIndex=$i; -          for($j=$i+1;$j<Count($arr);$j++){ to             if($minVal>$arr[$j]){ +                 $minVal=$arr[$j]; -                 $minIndex=$j; the             } *         } $         if($i!=$minIndex){Panax Notoginseng             $temp=$arr[$i]; -             $arr[$i] =$minVal; the             $arr[$minIndex] =$temp; +         } A     } the } +  - //Insert Sort method (small-to-large) $ functionInsertsort (&$arr){ $      for($i= 1;$i<Count($arr);$i++){ -         //value with Insert -         $insertVal=$arr[$i]; the         //the position subscript to compare -         $insertIndex=$i-1;Wuyi          while($insertIndex>=0 &&$insertVal<$arr[$insertIndex]){ the             //If the subscript of the comparison number is greater than or equal to 0, and the value inserted is smaller than the value being compared, the value to be compared will be moved back -             $arr[$insertIndex+1] =$arr[$insertIndex]; Wu             $insertIndex--; -         } About         //Insert Insertval $         if($insertIndex+1! =$i){ -             $arr[$insertIndex+1] =$insertVal; -         } -     } A } +  the  - $arr=Array(10,2,0,-23,90,-100,400); $ //Maopao_sort ($arr); the //selectsort ($arr); theInsertsort ($arr); the Echo Var_dump($arr)." <br> "; the Print_r($arr);

Quick Sort Method:

1<?PHP2 functionQuickSort (&$arr){3     if(Count($arr) >1){4         $k=$arr[0];5         $x=Array();6         $y=Array();7         $_size=Count($arr);8          for($i= 1;$i<$_size;$i++){9             if($arr[$i]<=$k){Ten                 $x[]=$arr[$i]; One}ElseIf($arr[$i]>$k){ A                 $y[]=$arr[$i]; -             } -         } the         $x=quicksort ($x); -         $y=quicksort ($y); -         return Array_merge($x,Array($k),$y); -}Else{ +         return$arr; -     } + } A?>

php--bubble, select, insert Sort method

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.