Example Analysis of PHP Quick Sort Algorithm

  • 2021-10-25 06:05:52
  • OfStack

In this paper, an example is given to describe PHP quick sorting algorithm. Share it for your reference, as follows:

Quick sort: In the unordered array $data, select any one value as the contrast, define i as the head retrieval index, j as the tail retrieval index,

Algorithm steps:

(1) Initialize the contrast ratio $value=$data[0] , $i=1 , $j=count($data)-1

(2) First, search and judge from the tail $data[$j] Is less than $value If it is not less than $j-- , continue to search until you find a $value Small coordinates

(3) At this time, head search and judgment are started $data[$i] Is greater than $value If it is not greater than, then $i++ , continue to search until you find a $value Large coordinates

(4) At this time $data[$j] And $data[$i] The values of are exchanged with each other, that is, the ratio $value Put the big one on the right, and put it better than $value Put the small one on the left

(5) Repeat 3, 4 until $i==$j

(6) At this time, the ratio has been compared $value Put the big one on the right, and put it better than $value The small one is placed to the left, and the middle coordinate position is determined as $i With a median value of $value , put $data[$i] The value of the is the same as that of the $j=count($data)-11 Because the median value is $value , need to put $value Move to the middle coordinate of the array

(7) The array is divided into two unordered arrays, and then recursively executes 1-6 until the array length is 1

Tips: The Chinese definition of quick sorting will be clearer under Baidu

Code:


<?php
header("Content-type: text/html; charset=utf-8");
function quickSort($data, $startIndex, $endIndex){
 if($startIndex < $endIndex){
  $value = $data[$startIndex]; //  Contrast ratio 
  $startT = $startIndex + 1;
  $endT = $endIndex;
  while ($startT != $endT) {
   //  Find coordinates smaller than the contrast ratio 
   while ($data[$endT] > $value && $endT > $startT){
    $endT--;
   }
   //  Find the left side larger than the contrast ratio 
   while ($data[$startT] < $value && $startT < $endT){
    $startT++;
   }
   if($endT > $startT){
    $temp =$data[$startT];
    $data[$startT] = $data[$endT];
    $data[$endT] = $temp;
   }
  }
  //  Prevent arrays from being sorted 
  if($data[$startT] < $value){
   $data[$startIndex] = $data[$startT];
   $data[$startT] = $value;
  }
  $data = quickSort($data, $startIndex, $startT - 1);
  $data = quickSort($data, $startT + 1, $endIndex);
  return $data;
 }else{
  return $data;
 }
}
$data = array(10, 5, 30, 22, 1, 42, 14, 34, 8, 13, 28, 36, 7);
$data = quickSort($data, 0, count($data) - 1);
var_dump($data);

Run results:

array(13) {
[0]= >
int(1)
[1]= >
int(5)
[2]= >
int(7)
[3]= >
int(8)
[4]= >
int(10)
[5]= >
int(13)
[6]= >
int(14)
[7]= >
int(22)
[8]= >
int(28)
[9]= >
int(30)
[10]= >
int(34)
[11]= >
int(36)
[12]= >
int(42)
}

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

More readers interested in PHP can check the topics of this site: "Summary of php Sorting Algorithm", "Tutorial on PHP Data Structure and Algorithm", "Summary of php Programming Algorithm", "Summary of php String (string) Usage", "Complete Collection of PHP Array (Array) Operation Skills", "Summary of PHP Common Traversal Algorithms and Skills" and "Summary of PHP Mathematical Operation Skills"

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


Related articles: