This article introduces the content of the quick sort recursive version of PHP implementation, has a certain reference value, now share to everyone, the need for friends can refer to
Today began to review the algorithm, even the most familiar with the fast line is not written out, shame, put the code, for later use it
function QSort (array & $a, $low, $high) { if ($low >= $high) { return; } $index = partition ($a, $low, $high); QSort ($a, $low, $index-1); QSort ($a, $index +1, $high);}
The element is mutually assignable than the Exchange efficiency function partition (array & $a, $low, $high) { $temp = $a [$low]; while ($low < $high) {while ($low < $high && $a [$high] >= $temp) { --$high; } $a [$low] = $a [$high]; while ($low < $high && $a [$low] <= $temp) { + + $low; } $a [$high] = $a [$low]; } $a [$low] = $temp; return $low;}
$a = [0,20,7,-1,6,2,6,2,8,9,0,1];
QSort ($a, 0, count ($a)-1);
Var_dump (Implode (', ', $a));
Results showing: -1,0,0,1,2,2,6,6,7,8,9,20