乐趣区

关于php:2快速排序PHP版

<?php
function quickSort(&$array,$left,$right) {if ($left > $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);
退出移动版