快速排序


$c = [12,13,11,10,4,1,5,56,87,45,332,123,4414,1312];

function sortBy($c)
{
    if (count($c) < 2) {
        return $c;
    }else {
        $d = $c[0];
        $less = [];
        $great = [];

        foreach ($c as $value) {
            if ($value < $d) {
                $less[] = $value;
            }
            if ($value > $d){
                $great[] = $value;
            }
        }

        return array_merge(sortBy($less),[$d],sortBy($great));
    }
}
var_dump(sortBy($c));

array (size=14)
  0 => int 1
  1 => int 4
  2 => int 5
  3 => int 10
  4 => int 11
  5 => int 12
  6 => int 13
  7 => int 45
  8 => int 56
  9 => int 87
  10 => int 123
  11 => int 332
  12 => int 1312
  13 => int 4414
posted @ 2020-03-29 09:19  Json159  阅读(147)  评论(0编辑  收藏  举报