Direct quick Sort is very simple, is an improvement of bubble sort, is the first time complexity reaches O (nlogn) algorithm, the worst time complexity is still O (n^2), it is also one of the ten greatest algorithms of the 21st century.
Quick sort there are three kinds, namely the balance of the fast row, the external fast row and the three-way base fast row, I do not separately introduced, the interest of the pot friends can see here.
Direct fast sort code directly on PHP
function QuickSort ($a) {if (count ($a) <=1) return $a; $l = $r = Array (); $n = Array_shift ($a); foreach ($a as $v) $v < $n Array_push ($l, $v): Array_push ($r, $v); Return Array_merge (QuickSort ($l), Array ($n), QuickSort ($r));}
Usage:
Echo (Implode (",", QuickSort ($a)));//$a is an unordered array, for example $a = range (1, 100); Shuffle ($a);
Hey, there's a better thought welcome comment
This article is from "A Tangled PHP programmer" blog, please be sure to keep this source http://johnsylm.blog.51cto.com/1480623/1429380