(2)快速排序-PHP版

 $right) return;
    $middle = partition($array,$left,$right);
    quickSort($array,$left,$middle-1);
    quickSort($array,$middle+1,$right);
}
function partition(&$array,$left,$right) {
    $v = $array[$left];
    $j = $left;
    for ($i=$left;$i<=$right;$i++) {
        if($array[$i] < $v) {
        $temp = $array[$j+1];
        $array[$j+1] = $array[$i];
        $array[$i] = $temp;
        $j++;
    }
 } 
    $temp = $array[$j];
    $array[$j] = $array[$left];
    $array[$left] = $temp;
    return $j;
}

function makeArray($n) {
    $array = [];
    for ($i = 0;$i<$n;$i++) {
        $array[$i] = mt_rand(0,$n);
    }
    return $array;
}

$array = makeArray(100);
quickSort($array,0,count($array)-1);
echo implode(',',$array);

你可能感兴趣的:(php快速排序)