Wrote three examples of PHP quick rows:
The first is low, but easiest and easiest to understand,
The second one is a one-way traversal search median method provided in the introduction to the algorithm,
The third is a bidirectional traversal to find the median value of the classic fast-ranked algorithm.
Three sets of algorithms are implemented and compared as follows:
Method One: This method is more intuitive, but loss of a lot of space at the cost of using a lower efficiency of the merge function. The least efficient of the three methods. In the worst case, the algorithm degrades (O (n*n))
function Quick_sort ($array) {
if (count ($array) <= 1) {return
$array;
}
$key = $array [0];
$rightArray = Array ();
$leftArray = Array ();
for ($i = 1; $i < count ($array); $i + +) {
if ($array [$i] >= $key) {
$rightArray [] = $array [$i];
} else {
$leftArray [] = $array [$i];
}
}
$leftArray = Quick_sort ($leftArray);
$rightArray = Quick_sort ($rightArray);
Return Array_merge ($leftArray, Array ($key), $rightArray);
Method Two: This algorithm from the introduction of the algorithm, called Nico Lomuto method (interested in Google has detailed description) use the most classic single direction traversal to find the median.
But this algorithm in the worst case (for example, the same value of the array, need to n-1, each partition requires O (n) time to remove an element) in the worst Case O (n*n)
function quick_sort (& $array, $start, $end) {if ($start >= $end) return;
$mid = $start;
for ($i = $start + 1; $i <= $end; $i + +) {if ($array [$i] < $array [$mid]) {$mid + +;
$tmp = $array [$i];
$array [$i] = $array [$mid];
$array [$mid] = $tmp;
}} $tmp = $array [$start];
$array [$start] = $array [$mid];
$array [$mid] = $tmp;
Quick_sort ($array, $start, $mid-1);
Quick_sort ($array, $mid + 1, $end); }
Method Three: This method is basically a textbook style of common writing, first from left to right to traverse less than the intermediate elements of the skip, while the right and left traversal encountered large elements skipped, and then if there is no crossover exchange values, continue to loop until the middle point found.
Note that the method still exchanges when dealing with the same element, so that there is an O (NLOGN) efficiency in the worst case. But in the following function, if you change the $array[$right] > $key to $array [$right] >= $key or change $array [$left] < $key to $array[$left] <= $key the worst case Not only will it degenerate into O (n*n). Moreover, in addition to the consumption of each comparison, there is an extra cost of n-time interaction. There are two other test sites for rote memorization: the middle two while interchangeable. Of course not interchangeable, because for the fast disk need an extra space to save the initial left value, so that the right and left side of the swap, the first to cover already saved
as the value of the left value, otherwise there will be problems. See this sentence $array[$left] = $array [$right]; $array [$right] = $key; Whether the meaning of the statement can be omitted. This sentence cannot be omitted, you can consider an extreme situation such as two values of the order (5,2), gradually look at the understanding.
Function Quick_sort_swap (& $array, $start, $end) {
if ($end <= $start) {return
;
}
$key = $array [$start];
$left = $start;
$right = $end;
while ($left < $right) {while
($left < $right && $array [$right] > $key) {
$right-;
}
$array [$left] = $array [$right];
while ($left < $right && $array [$left] < $key) {
$left + +;
}
$array [$right] = $array [$left];
}
$array [$right] = $key;
Quick_sort_swap ($array, $start, $right-1);
Quick_sort_swap ($array, $right +1, $end);
}
Method Three original code execution error, the author rewrite a:
Function QuickSort (& $arr, $start, $end) {
if ($start > $end) {return
;
}
$key = $arr [$start];
$left = $start;
$right = $end;
while ($left < $right) {while
($left < $right && $arr [$right]>= $key) {
$right-;
}
while ($left < $right && $arr [$left]<= $key) {
$left + +;
}
if ($left < $right) {
$tmp = $arr [$left];
$arr [$left] = $arr [$right];
$arr [$right] = $tmp;
}
}
$arr [$start] = $arr [$left];
$arr [$left] = $key;
QuickSort ($arr, $start, $right-1);
QuickSort ($arr, $right +1, $end);
}
Original link: Php to achieve a quick sort of three ways to share