For PHP in the array of elements to sort, this is very often used, before the project also has, and for several sorts we are using Asort Arsort and other PHP native functions, not to implement, so on a few functions to summarize, this will continue to supplement, Oneself can also review and summarize well.
Go directly to the code!
<?php/** * Insert sort (one-dimensional array) * each time a data element to be sorted is inserted into the appropriate position in the previously sequenced sequence, the sequence remains orderly until all the data elements to be sorted are inserted. */function Insertsort ($arr) {if (!is_array ($arr) | | count ($arr) = = 0) {return $arr;} $count = count ($arr); for ($i = 1; $i < $count; $i + +) {if (Isset ($arr [$i])) {$tmp = $arr [$i];//Gets the value of the latter element $j = $i-1;// Get the previous subscript while ($arr [$j] > $tmp) {//If the front one is larger than the next one, here is from small to large $arr[$j + 1] = $arr [$j];//Put the small element and the front of the swap until you move to the appropriate position, moving the next $arr[$ j] = $tmp; $j--;}}} return $arr;} /** * Select sort (one-dimensional array) * Each trip selects the smallest (largest) element from the data element to be sorted, placing the order at the end of the ordered sequence until all the data elements to be sorted are finished. */function Selectsort ($arr) {if (!is_array ($arr) | | count ($arr) = = 0) {return $arr;} $count = count ($arr), for ($i = 0; $i < $count, $i + +) {$k = $i; for ($j = $i + 1; $j < $count; $j + +) {if ($arr [$k] ; $arr [$j]) $k = $j; Find the smallest if ($k! = $i) {$tmp = $arr [$i]; $arr [$i] = $arr [$k]; $arr [$k] = $tmp;}}} return $arr;} /** * Bubble sort (one-dimensional array) * 22 compares the size of the data element to be sorted, and finds that two data elements are exchanged in reverse order until there are no reversed data elements */function Bubblesort ($array) {$count = count ($array if ($count <= 0) {return false;} For ($i = 0; $i < $count; $i + +) {for ($j = $count-1; $j > $i; $j-) {if ($array [$j] < $array [$j-1]) {//Compare the number found to exchange $tmp = $array [$j]; $array [$j] = $array [$j-1]; $array [$j-1] = $tmp;}}} return $array;} /** * Quick sort (one-dimensional array) */function QuickSort ($array) {if (count ($array) <= 1) {return $array;} $key = $array [0]; $left _arr = Array (); $right _arr = Array (); for ($i = 1; $i < count ($array); $i + +) {if ($array [$i] <= $key) {$left _arr[] = $array [$i];} else {$right _arr[] = $array [$i];}} $left _arr = QuickSort ($left _arr), $right _arr = QuickSort ($right _arr), return Array_merge ($left _arr, Array ($key), $right _arr);} /** * Sort by the value of the element * Strorder for the ordered ASC ascending desc descending */function sortbyval ($arr, $strOrder = ' asc ') {if (!is_array ($arr) | | co UNT ($arr) = = 0) {return $arr;} $arrReturn = Array (), foreach ($arr as $key = = $val) {$arrKey [] = $key; $arrVal [] = $val;} $count = count ($arrVal), if ($count) {//Create a sequential array of key for ($key = 0; $key < $count; $key + +) {$arrKeyMap [$key] = $key;} Sort the values for ($i = 0; $i < $count; $i + +) {for ($j = $count-1; $j > $i; $j-) {//< from small to large in this modified $bol = $strOrder = = ' ASC '? $arrVal [$j] < $arrVal [$j -1]: $arrVal [$j] > $arrVal [$j -1];if ($bol) {$tmp = $arrVal [$j]; $arrVal [$j] = $arrVal [$j-1]; $arrVal [$j-1] = $tm The p;//value of the bubble sort, causing the interaction of the array of key $keytmp = $arrKeyMap [$j]; $arrKeyMap [$j] = $arrKeyMap [$j-1]; $arrKeyMap [$j-1] = $keytmp;}} if (count ($arrKeyMap)) {foreach ($arrKeyMap as $val) {$arrReturn [] = $arrKey [$val];}} return $arrReturn;}} /** * Arrays are arranged by value using native functions */function arraysortbyval ($arr, $keys, $type = ' asc ') {$keysvalue = $new _array = Array (); foreach ($arr as $k = $v) {$keysvalue [$k] = $v [$keys];} if ($type = = ' asc ') {asort ($keysvalue);} else {arsort ($keysvalue);} Reset ($keysvalue), foreach ($keysvalue as $k = = $v) {$new _array[$k] = $arr [$k];} return $new _array;}? >
For the following 2 methods to sort the values of the array one is to implement the original PHP function is used, in fact, the order of a small amount of data generally on a single page of data is still possible, if it involves a large number of data sorting, recommendations can be integrated into the basic MySQL class.
Review and summary of sorting algorithm [PHP implementation]