算法系列-快速排序

phpman.song@gmail.com发布

定义

快速排序是用来排列一连串数字的算法

原理

  1. 从数列中挑出一个元素,称为基准
  2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以放在任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区操作
  3. 递归的把小于基准值元素的子数列和大于基准元素的子数列排序

PHP实现

PHP沙盒环境

function quickSort($arr)
{
    if (count($arr) <= 1)
        return $arr;
    $middle = $arr[0];
    $leftArray = [];
    $rightArray = [];

    for ($i = 1; $i < count($arr); $i++) {
        if ($arr[$i] > $middle)
            $rightArray[] = $arr[$i];
        else
            $leftArray[] = $arr[$i];
    }
    $leftArray = quickSort($leftArray);
    $leftArray[] = $middle;

    $rightArray = quickSort($rightArray);
    return array_merge($leftArray, $rightArray);
}
//例子
$numbers = [1,3,4,22,88,33,31,42];
print_r(quickSort($numbers));
分类: 算法

20 条评论

venlafaxine online · 2021年1月9日 下午5:49

Thanks for your own labor on this web site. My mother take interest in participating in research and it is simple to grasp why. My spouse and i notice all regarding the powerful way you produce valuable tips via your website and in addition recommend response from the others on this subject then our own princess is undoubtedly studying a whole lot. Take pleasure in the rest of the year. You have been performing a terrific job.

wellbutrin price · 2021年1月9日 下午6:54

I am commenting to let you know of the fine encounter my friend’s daughter found reading your site. She figured out many issues, not to mention what it’s like to have a wonderful giving mindset to have men and women quite simply have an understanding of selected hard to do subject areas. You really exceeded our expectations. Many thanks for giving the insightful, safe, explanatory and as well as easy thoughts on the topic to Jane.

venlor tablets · 2021年1月9日 下午11:50

A lot of thanks for each of your hard work on this web page. Kim really likes going through investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make powerful strategies on this web blog and as well as improve participation from people on that issue so our girl is in fact learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a really good job.

tofranil cheap · 2021年1月10日 上午9:30

My husband and i felt absolutely ecstatic Edward could complete his homework from the ideas he discovered from your very own site. It’s not at all simplistic just to be giving away helpful tips that men and women could have been making money from. And we discover we have the blog owner to appreciate because of that. Most of the explanations you’ve made, the easy blog menu, the friendships you assist to engender – it’s everything excellent, and it’s assisting our son and our family do think that content is brilliant, and that’s extraordinarily fundamental. Many thanks for all the pieces!

purchase doxepin · 2021年1月10日 上午10:32

I simply wished to thank you very much yet again. I do not know what I would have undertaken without those thoughts shared by you regarding this theme. Previously it was the terrifying scenario in my view, nevertheless understanding your well-written technique you solved the issue took me to weep with joy. Now i’m thankful for your support and thus trust you really know what an amazing job you were putting in training the others via your webpage. Most probably you haven’t met any of us.

发表评论

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