PHP implementation Insert Sort, PHP implementation sort
<?php/** * Insert Sort * @param array $a unordered collection * @return Array ordered set */function insertsort ($a) { $temp; $i; $j; $size _a = count ($a); # Start with the second element for ($i = 1; $i < $size _a; $i + +) { if ($a [$i] < $a [$i-1]) { $j = $i; # Saves the position of the current element $temp = $a [$i]; # The value of the current element # compares the left element, if found smaller than itself, move the element to the right, otherwise insert the element into the current position while ($j >0 && $temp < $a [$j-1]) { $a [$j] = $a [$j-1]; $j--; } # Insert Element $a [$j] = $temp; } } return $a;} /** * Get random number * @param integer $size number * @return integer */function randomnumber ($size = ten) { $rand = array (); Srand (Time (NULL)); for ($i = 0; $i < $size; $i + +) { Array_push ($rand, Mt_rand (0,1000)); } return $rand;} $a = Randomnumber (); Echo sprintf ("unsorted list%s\n", Implode ("", $a)); Echo sprintf ("Sorted list%s\n", Implode ("", INS Ertsort ($a)));
The above is the whole content of this article, I hope you can enjoy.
http://www.bkjia.com/PHPjc/975525.html www.bkjia.com true http://www.bkjia.com/PHPjc/975525.html techarticle PHP Implementation Insert Sort, PHP implementation sort php/** * Insert sort * @param array $a unordered collection * @return Array ordered set */function insertsort ($a) {$temp; $i; $j; $size _a = Count ...