Go directly to the code!
Copy Code code as follows:
<?php
/*
* Insert sort (one-dimensional array)
* Each time a data element to be sorted is inserted into the appropriate position in the previously sorted sequence, so that the sequence remains in order until the data element to be sorted is fully 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 front subscript
while ($arr [$j] > $tmp) {//If the front one is bigger than the back one, here is a small to large
$arr [$j +1] = $arr [$j]; Put the small elements in front of the swap until you move to the appropriate position, move 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, sequentially 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 Compare the size of the data element to be sorted, and find that the order of two data elements is exchanged until there is no inverse data element
*/
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 found number 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 order ASC Ascending desc Descending
*/
function Sortbyval ($arr, $strOrder = ' ASC ')
{
if (!is_array ($arr) | | | | COUNT ($ARR) ==0)
{
return $arr;
}
$arrReturn = Array ();
foreach ($arr as $key => $val)
{
$arrKey [] = $key;
$arrVal [] = $val;
}
$count = count ($arrVal);
if ($count)
{
Order array of key creation
for ($key =0; $key < $count; $key + +)
{
$arrKeyMap [$key] = $key;
}
To sort a value
for ($i =0; $i < $count; $i + +)
{
for ($j = $count-1; $j > $i; $j-)
{
< from small to large arrange movements 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] = $tmp;
The bubble sort of values that causes 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;
}
}
/**
* Use native functions to arrange arrays by value
*/
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 for sorting the values of the array, one is implemented by itself, one that uses the native PHP function, in fact, sorting for a small amount of data in general on a single page of data can still be, if it involves a large number of data sorting, the proposal can be integrated into the MySQL base class to carry out.