算法系列-快速排序

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 条评论

order propecia · 2021年1月8日 上午7:45

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.

CELEXA CITALOPRAM · 2021年1月8日 下午12:55

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!

order amitriptyline · 2021年1月9日 上午1:01

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.

Lokuspams · 2021年1月9日 上午6:16

usa pharmacy erectile dysfunction india pharmacy

nortriptyline without a prescription · 2021年1月9日 上午7:46

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.

发表评论

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