快速排序

Posted json159

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速排序相关的知识,希望对你有一定的参考价值。


$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

以上是关于快速排序的主要内容,如果未能解决你的问题,请参考以下文章

算法排序之堆排序

前端开发工具vscode如何快速生成代码片段

前端开发工具vscode如何快速生成代码片段

如何使用sublime代码片段快速输入PHP头部版本声明

代码片段如何使用CSS来快速定义多彩光标

vs2003:快速片段工具