算法系列-快速排序

定义

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

原理

  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));

39 comments

  1. I feel that is among the so much significant info for me.

    And i’m happy studying your article. However wanna commentary on some common issues, The web site style is perfect, the articles is actually
    excellent : D. Just right process, cheers

  2. Great article! We will be linking to this great content on our site.
    Keep up the good writing.

  3. My spouse and I stumbled over here from a different page and thought I might as well
    check things out. I like what I see so now i am following
    you. Look forward to looking at your web page repeatedly.
    adreamoftrains web hosting companies

  4. Hi, the whole thing is going fine here and ofcourse every
    one is sharing data, that’s in fact fine, keep up writing.

  5. Tremendous things here. I’m very glad to look your
    article. Thank you a lot and I’m taking a
    look ahead to touch you. Will you kindly drop me
    a e-mail?

  6. I wanted to check up and let you know how really I liked discovering your blog today.

  7. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render worthwhile tricks via your website and therefore welcome response from website visitors on this topic while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a wonderful job.

  8. Needed to write you this very small word to help thank you very much yet again with the unique tricks you have shown on this website. This is so wonderfully open-handed with you to supply without restraint what exactly some people would have supplied for an electronic book to make some profit on their own, specifically since you might well have tried it in the event you wanted. Those tips likewise worked to provide a great way to understand that the rest have the same zeal the same as my very own to understand way more with regards to this problem. I think there are thousands of more pleasurable times up front for those who take a look at your website.

  9. A lot of thanks for all your valuable efforts on this site. Kate loves engaging in investigation and it’s really easy to understand why. I know all concerning the dynamic medium you give important secrets on this web blog and as well as foster participation from others on that content plus our daughter is always discovering a lot. Have fun with the remaining portion of the new year. You are conducting a great job.

  10. My spouse and i got quite thankful when Raymond managed to round up his survey with the precious recommendations he was given while using the weblog. It is now and again perplexing to simply possibly be releasing tips and hints which usually the others might have been trying to sell. We really take into account we now have you to thank for this. Those illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly unbelievable, and it is making our son in addition to us understand this topic is satisfying, which is certainly very vital. Thanks for the whole thing!

  11. Thanks for your own work on this website. My mother take interest in setting aside time for research and it is simple to grasp why. My spouse and i notice all relating to the powerful way you provide very helpful tips via your website and in addition recommend response from visitors on this subject matter while our own simple princess is undoubtedly studying so much. Take pleasure in the rest of the year. You have been performing a tremendous job.

  12. I’m commenting to let you be aware of of the awesome discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such effective, dependable, edifying and cool guidance on that topic to Emily.

  13. 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.

  14. 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.

  15. 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.

  16. 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!

  17. 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.

  18. Hello. fantastic job. I did not expect this. This is a fantastic story. Thanks! https://hairlossbimedi.com hair loss medications

  19. You got a very good website, Glad I found it through google. https://hcvmedi.com buy Hepatitis C Virus medication

  20. You got a very great website, Gladiola I discovered it through google. https://hcvmedi.com HCV drugs for sale

  21. I am continually invstigating online for tips that can assist me. Thanks! https://hivmedi.com aids drugs

  22. fantastic publish, very informative. I’m wondering why the opposite specialists of this sector don’t understand this. You must proceed your writing. I’m confident, you’ve a great readers’ base already! https://anxietymedi.com Anxiety medication

  23. Hi there, just became alert to your blog through Google, and found that it’s truly informative. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Many people will be benefited from your writing. Cheers! https://hypothyroidismmed.com/ buy Hypothyroidism medication

  24. Hi there, just became alert to your blog through Google, and found that it’s truly informative. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Many people will be benefited from your writing. Cheers! https://hypothyroidismmed.com/ Hypothyroidism drugs over the counter

  25. Thanks for the good writeup. It in fact was once a enjoyment account it.
    Glance complex to far brought agreeable from you! By the way, how could we be in contact?

  26. Hi, i think that i saw you visited my weblog thus i came to “return the favor”.I’m attempting to find things to improve my website!I suppose its
    ok to use a few of your ideas!!

  27. Excellent post. I was checking constantly this
    blog and I am impressed! Extremely helpful info
    particularly the last part 🙂 I care for
    such information much. I was looking for this certain information for a very long time.
    Thank you and good luck.

  28. Paragraph writing is also a fun, if you be familiar with after that you can write
    or else it is complicated to write.

  29. This is very interesting, You’re a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your excellent post.
    Also, I have shared your web site in my social networks!

  30. Thank you for any other informative site. The place else may just I am getting that kind of info written in such a perfect approach?
    I have a project that I’m simply now working on,
    and I’ve been on the look out for such info.

  31. We’re a group of volunteers and starting a new scheme in our community.
    Your website provided us with valuable info to work on.
    You’ve done a formidable job and our whole community will be grateful to you.

Leave a comment

Your email address will not be published. Required fields are marked *