<?php/** * Created by Phpstorm. * User:chm * DATE:2016/4/1 * time:19:35 *///Insert sort feature is in the order of the side is ranked, the other side of a and sequential data comparison, each time the contrast insert a function Charu ($arr) { $len = C Ount ($arr); First, give an original array echo "original array as:"; for ($y = 0; $y < $len; $y + +) { echo $arr [$y]; } echo "<br>"; echo "Start sorting below"; echo "<br>"; Outer loop null round for ($i = 1; $i < $len; $i + +) { //Compare new number and previous sequence, find a good position to insert $x = $arr [$i]; for ($j = $i-1; $j >= 0; $j-) { if ($x < $arr [$j]) { //discovers the inserted element to be small, swap position, swap the element behind with the previous element $arr [$j +1] = $arr [$j]; $arr [$j] = $x; } If you encounter an element that does not need to be moved, the preceding one does not need to be compared again because it is an array that is already lined up. else {break ; } } The For loop is used for the output array, starting at the end of the first sort for ($y = 0; $y < $len; $y + +) { echo $arr [$y]; } echo "<br>"; } } $ARRAYHA = Array (3,1,6,4,5,2); Charu ($ARRAYHA);
<?php/** * Created by Phpstorm. * User:chm * DATE:2016/4/1 * time:18:51 * *//bubble algorithm has a large number of sink, decimals surfaced, the idea of the same nature, this method is used in large number sink function Maopao ($arr) { $len = count ($a RR); First, give an original array echo "original array as:"; for ($y = 0; $y < $len; $y + +) { echo $arr [$y]; } echo "<br>"; echo "Start sorting below"; echo "<br>"; Used to record the Bubbling round for ($i = 1; $i < $len; $i + +) { //To mark comparisons for ($k = 0; $k < $len-$i; $k + +) { //Compare size if ($arr [$k]> $arr [$k +1]) { $x = $arr [$k +1]; $arr [$k +1] = $arr [$k]; $arr [$k] = $x; } } The below for loop is used for the output array, starting with the first sort for ($y = 0; $y < $len; $y + +) { echo $arr [$y]; } echo "<br>"; } }//Call method $arrayha = Array (3,1,6,4,5,2); Maopao ($ARRAYHA);
<?php/** * Created by Phpstorm. * User:chm * DATE:2016/4/1 * time:19:21 *///Select Sort, there should also be a maximum minimum selection method bar, the idea is still the same, the method uses a small value to select function Xuanze ($arr) { $len = Coun T ($arr); First, give an original array echo "original array as:"; for ($y = 0; $y < $len; $y + +) { echo $arr [$y]; } echo "<br>"; echo "Start sorting below"; echo "<br>"; Record round for ($i = 0; $i < $len-1; $i + +) { //Select the sort tag $s, which is used to mark the minimum value $s = $i; for ($j = $i +1; $j < $len; $j + +) { if ($arr [$s] > $arr [$j]) { $s = $j; } } If the tag is not in the initial position, the exchange tag and the previous original tag data if ($s! = $i) { $x = $arr [$s]; $arr [$s] = $arr [$i]; $arr [$i] = $x; } The below for loop is used for the output array, starting with the first sort for ($y = 0; $y < $len; $y + +) { echo $arr [$y]; } echo "<br>"; } } $arrayha = Array (3,1,6,4,5,2); Xuanze ($ARRAYHA);
<?php/** * Created by Phpstorm. {///first determine if you need to continue sorting (that is, there are several data in the array) $len = count ($arr); if ($len <= 1) {return $arr; }//Select the first element as the intermediate standard value $middle = $arr [0]; Traverse all elements outside of the standard value, put in two arrays according to the size relationship//Initialize two arrays for sorting $left = Array (); Array less than the intermediate standard value $right = array (); An array greater than the standard value for ($i =1; $i < $len; $i + +) {if ($middle > $arr [$i]) {//Put in the left array $left [] = $a rr[$i]; } else {//put to the right $right [] = $arr [$i]; }}//Do the same for the left and right arrays by recursively calling this function $left = Kuaisu ($left); $right = Kuaisu ($right); Merge return Array_merge ($left, Array ($middle), $right);} Give an original array $arrayha = Array (3,1,6,4,5,2), $l = count ($ARRAYHA), echo "original array:"; for ($y = 0; $y < $l; $y + +) {echo $arrayha [$y ];} echo "<br>"; Call Method $s = Kuaisu ($ARRAYHA); The For loop below is used for the output array, starting with the first sort of echo "quick sort after:"; echo "<br> "; for ($y = 0; $y < $l; $y + +) {echo $s [$y];}
Bubble Select Insert Fast, four most basic sort algorithm implementations