Analysis of PHP quick sorting principle and implementation method, PHP sorting principle implementation_PHP tutorial

WBOY
Release: 2016-07-12 08:50:48
Original
804 people have browsed it

Analysis of PHP quick sorting principle and implementation method, implementation of PHP sorting principle

The example in this article describes the PHP quick sorting method. Share it with everyone for your reference, the details are as follows:

<&#63;php
$n = array('13','14','55','10','54','2','79','106','89','90','22','60','111','77777','-110','-10','123');
function partition($n,$left,$right)
{
  global $n;
  $pivot = $n[$left];
  $lo=$left;
  $hi=$right+1;
  while($lo+1!=$hi) {
    if($n[$lo+1]<$pivot)
      $lo++;
    else if($n[$hi-1]>$pivot)
      $hi--;
    else{
      $t=$n[$lo+1];
      $n[$lo+1]=$n[$hi-1];
      $n[$hi-1]=$t;
      $lo++;
      $hi--;
    }
  }
  $n[$left]=$n[$lo];
  $n[$lo]=$pivot;
  return $lo;
}
function quicksort($n,$left,$right)
{
  global $n;
  $dp = 0;
  if ($left<$right) {
     $dp=partition($n,$left,$right);
     quicksort($n,$left,$dp-1);
     quicksort($n,$dp+1,$right);
  }
}
quicksort($n,0,sizeof($n)-1);
print_r($n);
&#63;>

Copy after login

Quick sort is an improvement on bubble sort. Its basic idea is to divide the data to be sorted into two independent parts through one-way sorting. All the data in one part is smaller than all the data in the other part, and then the two parts of the data are sorted sequentially. For quick sorting, the entire sorting process can be performed recursively, so that the entire data becomes an ordered sequence.

Assume that the array to be sorted is A[1]...A[N], first select any data (usually the first data) as the key data, and then put all numbers smaller than it in front of it , all numbers larger than it are placed behind it. This process is called one-position quick sort. The algorithm of quick sort is:

1) Set two variables I and J. When sorting starts, I: =1, J: =N;
2). Use the first array element as the key data and assign it to X, that is, X: =A[1];
3) Search forward from J, that is, search forward from the back (J:=J-1), find the first value less than X, and exchange the two;
4) Search backward from I, that is, search from front to back (I:=I 1), find the first value greater than X, and exchange the two;
5) Repeat steps 3 and 4 until I=J;

Quick sorting is to call this process recursively - split the data sequence with 49 as the midpoint, perform similar quick sorting on the previous part and the latter part respectively, thereby completing the quick sorting of the entire data sequence, and finally put this data sequence into Become an ordered sequence

Supplement: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:

php code online formatting and beautification tool:

http://tools.jb51.net/code/phpformat

In addition, since php belongs to the C language style, the following tool can also format php code:

C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json

Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133075.htmlTechArticleAnalysis of PHP quick sorting principle and implementation method, PHP sorting principle implementation. This article describes the PHP quick sorting method with examples. Share it with everyone for your reference, the details are as follows: php$n = array('13',...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template