This article mainly introduces the quick sort of PHP code, has a certain reference value, and now share to everyone, the need for friends can refer to
First, the principle of introduction
Actually, it's simple.
An array [6, 1, 2, 7, 9, 3, 4, 5, ten, 8]
A, find the first 6 (any line)
b, the smaller than 6, 6 larger than the separate, into 1 arrays
C, b operation to get two arrays, and then repeat the AB operation, eventually merging arrays
Two, the above code
/** * Quick Sort */function Quick_sort ($arr) { $length = count ($arr); if ($length <= 1) { return $arr; } $left = $right = []; for ($i = 1; $i < $length; $i + +) { if ($arr [$i] < $arr [0]) { $left [] = $arr [$i]; } else { $right [] = $arr [$i]; } } Recursive call $left = Quick_sort ($left); $right = Quick_sort ($right); Return Array_merge ($left, [$arr [0]], $right);} $arr _data = [6, 1, 2, 7, 9, 3, 4, 5, ten, 8];p Rint_r (Quick_sort ($arr _data));
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!