Php fast sorting principle and implementation method analysis, php sorting principle implementation. Php quick sorting principle and implementation method analysis, php sorting principle implementation this article describes the php quick sorting method. For your reference, see php $ narray (13. Analysis of php's fast sorting principle and implementation method, and php's sorting principle implementation.
This article describes the php quick sorting method. We will share this with you for your reference. The details are 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);?>
Quick sorting is an improvement of bubble sorting. Its basic idea is: split the data to be sorted into two independent parts by means of a lie-down sorting, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by the second method. the whole sorting process can be recursive to convert the entire data into an ordered sequence.
Assume that the array to be sorted is A [1]... A [N], first select A data (usually the first data) as the key data, and then put all the numbers smaller than it in front of it, all the numbers that are larger than it are placed behind it. this process is called "one lie" and "fast sorting. The following algorithm is used to sort data quickly:
1) set two variables, 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) start from J to search forward, that is, start from the back to search forward (J: = J-1), find the first value less than X, the two exchange;
4) search backward from I, that is, search backward from the beginning (I: = I + 1), find the first value greater than X, and exchange the two;
5) repeat steps 3rd and 4 until I = J;
Fast sorting is a recursive call to this process-split the data sequence with 49 as the midpoint and perform similar fast sorting on the previous and subsequent parts respectively to complete the fast sorting of all data sequences, finally, the data sequence is converted into an ordered sequence.
Supplement: The editor recommends a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat
In addition, because 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 tools:
Http://tools.jb51.net/code/ccode_html_css_json