This article is to share the content of PHP four of the entry-level ranking algorithm, has a certain reference value, the need for friends can refer to
four entry-level sorting algorithms:bubble Sort, select sort, insert sort, quick sort.
$a = [5,2,4,1,6,5,3,8];function A ($a) {//bubble sort//$temp = 0; for ($i =0; $i <count ($a)-1; $i + +) {for ($j =0; $j <count ($a)-$i, $j + +) {if ($a [$j]> $a [$j +1]) { $temp = $a [$j]; $a [$j] = $a [$j +1]; $a [$j +1] = $temp; }}} print_r ($a);} A ($a); function order ($arr) {//select sort//define intermediate variable $temp = 0; $count = count ($arr); for ($i =0; $i < $count-1; $i + +) {//define minimum position $minIndex = $i; for ($j = $i +1; $j < $count; $j + +) {if ($arr [$j] < $arr [$minIndex]) {$minIndex = $j; }} if ($i! = $minIndex) {$temp = $arr [$i]; $arr [$i] = $arr [$minIndex]; $arr [$minIndex] = $temp; }} return $arr;} echo "<pre>"; Print_r (Order ($a)), function Swap (array & $arr, $a, $b) {//Direct insert Sort $temp = $arr [$a]; $arr [$a] = $arr [$b]; $arr [$b] = $temp;} function Insertsort (array & $arr) {$count = count ($arr); ArrayThe first element as an already existing ordered table for ($i = 1; $i < $count; $i + +) {$temp = $arr [$i]; Set Sentinel for ($j = $i-1; $j >= 0 && $arr [$j] > $temp; $j-) {$arr [$j + 1] = $arr [$j]; Record Move} $arr [$j + 1] = $temp; Insert into the correct position}}insertsort ($a); Var_dump (' <pre> ', $a), function Quick_sort ($arr)//functions for fast ordering {//Determine if the parameter is an array if (!is_ Array ($arr)) return false; Recursive exit: Array length is 1, return array directly $length =count ($arr); if ($length <=1) return $arr; array element has multiple, then define two empty array $left = $right =array (); Using a For loop, use the first element as the object of comparison for ($i =1; $i < $length; $i + +) {//To determine the size of the current element if ($arr [$i]< $arr [0]) { $left []= $arr [$i]; }else{$right []= $arr [$i]; }}//Recursive call $left =quick_sort ($left); $right =quick_sort ($right); Merge all results into return Array_merge ($left, Array ($arr [0]), $right);} Var_dump (' <pre> ', $a);//Call echo "<pre>";p rint_r (Quick_sort ($a));