The example of this article describes the PHP quick Sort method. Share to everyone for your reference, specific 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);
Print_r ($n);
? >
A quick sort is an improvement to the bubble sort. Its basic idea is to divide the data into two separate parts by a lie sort a part of all the data is smaller than the other part of all the data, and then the two parts of the data are sorted quickly, the whole sort process can be recursive, so as to achieve the entire data into an ordered sequence.
Suppose the array to sort is a[1] ... A[n], first select one of the data (usually the first data) as the key data, and then put all the number smaller than it before it, all the number larger than it is placed behind it, this process is called a lie fast sort. A quick sort of lying algorithm 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, assigning value to X, i.e. x:=a[1];
3), starting from the beginning of the search, that is, from the start forward search (j:=j-1), find the first value less than x, the exchange between the two;
4), from I start backward search, that is, from the front start backward search (i:=i+1), find the first value greater than X, both exchange;
5, repeat the 3rd, 4 steps until i=j;
A quick sort is a recursive call to this process--dividing the data sequence by a midpoint of 49, making a similar quick sort of the preceding and subsequent sections, to complete the fast sequencing of all the data sequences, and finally turning this data sequence into an ordered sequence
Add: Small knitting here recommend a site for the layout of the PHP format landscaping tools to help you in the future of PHP programming code layout:
PHP code online format Landscaping tools:
Http://tools.jb51.net/code/phpformat
In addition, because PHP belongs to the C language style, the following tool can also be used to format PHP code:
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json
More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.