Home  >  Article  >  Backend Development  >  PHP skills: PHP quick sorting algorithm example analysis

PHP skills: PHP quick sorting algorithm example analysis

无忌哥哥
无忌哥哥Original
2018-07-12 14:17:111372browse

This article mainly introduces the PHP quick sorting algorithm, and analyzes the principles, steps and related PHP definitions and usage techniques of quick sorting in the form of examples. Friends in need can refer to the following

The examples in this article describe PHP quick sort algorithm. Share it with everyone for your reference, the details are as follows:

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

Algorithm steps:

(1) Initialization comparison value $value=$data[0], $i=1, $j=count($data)-1

(2) First search from the end to determine whether $data[$j] is less than $value, if not less than $j--, continue searching until you find coordinates that are smaller than

$value

(3) This Start header retrieval, determine whether $data[$i] is greater than $value, if not, $i , continue retrieval until the ratio # is found ##$valueLarge coordinates

(4) At this time, the values ​​​​of

$data[$j] and $data[$i] are exchanged. , that is, put the ones larger than $value to the right, and the ones smaller than $value to the left

(5) Repeat 3 and 4 until

$i==$j

(6) At this time, the one larger than

$value has been placed on the right, and the one smaller than $value has been placed on the right. On the left, the middle coordinate position is determined to be $i, the middle value is $value, and the value of $data[$i] is compared with $ Value exchange of data[0], because the intermediate value is $value, you need to move $value to the middle coordinate of the array

(7) Array Divide it into two unordered arrays on the left and right, and then execute 1-6 recursively until the array length is 1

Tips: The Chinese definition of quick sort will be clearer in Baidu

code :

<?php
header("Content-type: text/html; charset=utf-8");
function quickSort($data, $startIndex, $endIndex){
 if($startIndex < $endIndex){
  $value = $data[$startIndex]; // 对比值
  $startT = $startIndex + 1;
  $endT = $endIndex;
  while ($startT != $endT) {
   // 找到比对比值小的坐标
   while ($data[$endT] > $value && $endT > $startT){
    $endT--;
   }
   // 找到比对比值大的左边
   while ($data[$startT] < $value && $startT < $endT){
    $startT++;
   }
   if($endT > $startT){
    $temp =$data[$startT];
    $data[$startT] = $data[$endT];
    $data[$endT] = $temp;
   }
  }
  // 防止数组已经排序好的情况
  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 result:

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)
}

The above is the detailed content of PHP skills: PHP quick sorting algorithm example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn