Example of PHP Recursive Quick Sort Method

  • 2021-08-28 19:34:29
  • OfStack

In this paper, an example is given to describe the method of fast sorting by PHP recursion. Share it for your reference, as follows:

First of all, we need to understand the principle of quick sorting under 1: Find any 1 element in the current array (1 generally chooses the first element), as a standard, create two new empty arrays, traverse the whole array elements, if the traversed elements are smaller than the current elements, then put them in the left array, otherwise put them in the right array, and then do the same for the new array.

It is not difficult to find that this conforms to the principle of recursion, so we can use recursion to achieve it.

To use recursion, you need to find recursion points and recursion exits:

Recursive point: If the elements of the array are greater than 1, we need to decompose them again, so our recursive point is that the number of newly constructed elements of the array is greater than 1

Recursive exit: When do we no longer need to sort new arrays? That's when the number of elements in the array becomes 1, so this is our exit.

Understand the principle, look at the code implementation under 1 ~


<?php
// Quick sort 
// Array to be sorted 
$arr=array(6,3,8,6,4,2,9,5,1);
// Function to realize quick sorting 
function quick_sort($arr)
{
    // Determine whether the parameter is 1 Array of numbers 
    if(!is_array($arr)) return false;
    // Recursive exit : Array length is 1 Returns the array directly 
    $length=count($arr);
    if($length<=1) return $arr;
    // Array elements have multiple , Two empty arrays are defined 
    $left=$right=array();
    // Use for Loop to traverse, and put the first 1 Elements as objects for comparison 
    for($i=1;$i<$length;$i++)
    {
      // Determine the size of the current element 
      if($arr[$i]<$arr[0]){
        $left[]=$arr[$i];
      }else{
        $right[]=$arr[$i];
      }
    }
    // Recursive invocation 
    $left=quick_sort($left);
    $right=quick_sort($right);
    // Merge all the results 
    return array_merge($left,array($arr[0]),$right);
}
// Call 
echo "<pre>";
print_r(quick_sort($arr));

Run results:


Array
(
  [0] => 1
  [1] => 2
  [2] => 3
  [3] => 4
  [4] => 5
  [5] => 6
  [6] => 6
  [7] => 8
  [8] => 9
)

PS: Here we recommend another demonstration tool about sorting for your reference:

Online animation demonstrates insert/select/bubble/merge/hill/quick sort algorithm process tool:
http://tools.ofstack.com/aideddesign/paixu_ys

For more readers interested in PHP related contents, please check the special topics of this site: Summary of php Sorting Algorithm, Tutorial of PHP Data Structure and Algorithm, Summary of php Programming Algorithm, Encyclopedia of PHP Array (Array) Operation Skills, Summary of php String (string) Usage, Summary of PHP Common Traversal Algorithms and Skills and Summary of PHP Mathematical Operation Skills

I hope this article is helpful to everyone's PHP programming.


Related articles: