Review and summary of PHP sorting algorithm _php tutorial

Source: Internet
Author: User
Go directly to the code!
Copy CodeThe code is as follows:
/*
* Insert sort (one-dimensional array)
* Each time a data element to be sorted is inserted into the appropriate position in the previously sorted 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 back one, this is from small to large
$arr [$j +1] = $arr [$j]; Move the small element and the front of the swap until it moves 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, placed in the final order of the ordered sequence, until all the data elements are sorted out.
*/
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 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 swap
$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 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 order
*/
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)
{
Create a sequential array of keys
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-)
{
// <从小到大排列 升降在这修改
$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 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 sorted 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.

http://www.bkjia.com/PHPjc/324976.html www.bkjia.com true http://www.bkjia.com/PHPjc/324976.html techarticle go directly to the code! Copy the code as follows:? PHP/* * Insert sort (one-dimensional array) * each time a data element to be sorted is inserted into the appropriate order in the preceding sequence of columns ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.