Review and summary of PHP sorting algorithm

  • 2020-05-16 06:26:34
  • OfStack

Directly on the code!
 
<?php 
/* 
*  Insertion sort ( 1 Dimensional array)  
*  Each time will be 1 The data elements to be sorted are inserted into the appropriate positions in the previously sorted sequence, so that the sequence is still in order; 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]; // After obtaining 1 The value of the element  
   $j = $i - 1; // Get the preceding subscript  
   while($arr[$j] > $tmp){ // If the previous 1 Than the back 1 A big,   This is from small to big  
    $arr[$j+1] = $arr[$j]; // Swap the smaller element with the one in front until it's in the right place, under the move 1 a  
    $arr[$j] = $tmp; 
    $j--; 
   } 
} 
} 
return $arr; 
} 
   
/* 
*  Selection sort ( 1 Dimensional array)  
*  every 1 The loop selects the smallest (largest) of the data elements to be sorted 1 Elements, placed at the end of the sorted sequence, until all the data elements to be sorted are sorted.  
*/ 
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 ( 1 Dimensional array)  
*  Pairwise compare the size of the data elements to be sorted, and if two data elements are found in opposite order, they are exchanged until there are no data elements in reverse order  
*/ 
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 Numbers found and swap them  
    $tmp = $array[$j]; 
    $array[$j] = $array[$j-1]; 
    $array[$j-1] = $tmp; 
   } 
} 
} 
return $array; 
} 
   
/* 
*  Quicksort ( 1 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  Is the order of arrangement  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 key Order array of  
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   The lift is modified here  
$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; 
// Value of bubble sort, caused key The interaction of the array  
$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 two sorting methods of array values, one is self-implemented and the other USES the native PHP function. In fact, sorting is still ok for a small amount of data, such as a single page of data. If sorting involves a large amount of data, it is recommended to integrate it into the basic class of MYSQL.

Related articles: