<?php
function partion(&$arr, $low, $high)
{
$privorKey = $arr[$low];
while ($low < $high) {
while ($low < $high && $arr[$high] >= $privorKey) {
$high --;
}
$arr[$low] = $arr[$high];
while ($low < $high && $arr[$low] <= $privorKey) {
$low ++;
}
$arr[$high] = $arr[$low];
}
$arr[$low] = $privorKey;
return $low;
}
function qSort(&$arr, $low, $high) {
if ($low >= $high) {
return ;
}
$pos = partion($arr, $low, $high);
qSort($arr, $pos + 1, $high);
qSort($arr, $low, $pos - 1);
}
$arr = [1,6,7,99,23,4,1,94,5];
qSort($arr, 0, count($arr) - 1);
print_r($arr);
网友评论