冒泡排序、快速排序

无聊写个基础的排序玩玩。

echo "<pre>";
$d = [66,11,23,25,6,22,88,23,7,9];
$len = count($d);
print_r($d);
for($y=0;$y<$len-1;$y++){
    echo "\n";
    for($i=0;$i<$len-1-$y;$i++){
        if($d[$i]>$d[$i+1]){
            $tmp = $d[$i];
            $d[$i] = $d[$i+1];
            $d[$i+1] = $tmp;
        }
        echo $d[$i].' ';
    }
}
echo "\n";
print_r($d);
echo "</pre>";

echo "<pre>";
$d = [66,11,23,25,6,22,88,23,7,9];
print_r($d);
$exec = function ($arr) use(&$exec){
    if(count($arr)>1){
        $cur = $arr[0];
        $l = [];
        $r = [];
        foreach ($arr as $v){
            if($v<$cur){
                $l[] = $v;
            }
            if($v>$cur){
                $r[] = $v;
            }
        }
        echo count($l).'-'.count($r)."\n";
        return array_merge($exec($l),[$cur],$exec($r));
    }else{
        return $arr;
    }
};
echo "\n";
print_r($exec($d));
echo "</pre>";

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Captcha Code