PHP Quick Sort principle and implementation method analysis, the PHP sorting principle realizes
In this paper, a quick ordering method for PHP is described. Share to everyone for your reference, as follows:
<?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);p Rint_r ($n);? >
A quick sort is an improvement to the bubbling sort. Its basic idea is: by a lying sort will be sorted into two separate parts of the data, one part of all the data is smaller than the other part of all the data, and then the second method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
Suppose the array to be sorted is a[1] ... A[n], the first arbitrary selection of data (usually the first data) as the key data, and then put all the smaller than its number in front of it, all the larger than its number is placed behind it, this process is called a lie fast sort. A quick-sort algorithm for lying down is:
1), set two variables I, J, the beginning of the sorting i:=1,j:=n;
2), with the first array element as the key data, assigns the value to X, namely x:=a[1];
3), starting from J forward search, that is, after the start of the forward search (j:=j-1), find the first value less than X, the two exchange;
4), from I start backward search, that is, to start backward search (i:=i+1), find the first value greater than X, the two exchange;
5), repeat the 3rd and 4 steps until i=j;
A quick sort is a recursive invocation of this process--dividing the data sequence by a 49 midpoint, a similar quick ordering of the previous and subsequent parts, and a quick ordering of all the data sequences, and finally turning this data series into an ordered sequence
Add: small make up here to recommend a site of the PHP format beautification of the layout tools to help you in the future of PHP programming code layout:
PHP Code online format Beautification tool:
Http://tools.jb51.net/code/phpformat
In addition, because PHP is a C language style, the following tool can also be used to implement the PHP code format:
C Language Style/html/css/json code formatting beautification tool:
Http://tools.jb51.net/code/ccode_html_css_json
More about PHP related content readers can view this site topic: "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Math Skills Summary, "PHP Regular Expression Usage summary", "PHP Operations and Operator Usage Summary", "PHP string Usage Summary" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1133075.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133075.html techarticle PHP Fast sorting principle and implementation method analysis, the PHP sorting principle implementation of this article describes the PHP fast sorting method. Share to everyone for your reference, as follows: php$n = Array (',... '