Algorithmic Heights第30题:Quick Sort

Problem

Comparing the algorithms for sorting and “Median” finding we notice that, beyond the common divide-and-conquer philosophy and structure, they are exact opposites. “Merge Sort” splits the array in two in the most convenient way (first half, second half), without any regard to the magnitudes of the elements in each half; but then it works hard to put the sorted subarrays together. In contrast, the median algorithm is careful about its splitting (smaller numbers first, then the larger ones), but its work ends with the recursive call.

Quick sort is a sorting algorithm that splits the array in exactly the same way as the median algorithm; and once the subarrays are sorted, by two recursive calls, there is nothing more to do. Its worst-case performance is , like that of median-finding. But it can be proved that its average case is ; furthermore, empirically it outperforms other sorting algorithms. This has made quicksort a favorite in many applications— for instance, it is the basis of the code by which really enormous files are sorted.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Given: A positive integer  and an array  of integers from  to .

Return: A sorted array .

比较排序算法和“中位数”发现算法,我们注意到,除了常见的 分而治之的原理和结构之外,它们是完全相反的。“合并排序”以最方便的方式将数组分为两个部分(上半部分,下半部分),而无需考虑每半部分中元素的大小;但随后很难将排序后的子数组放在一起。相反,中值算法在拆分时会比较小心(首先是较小的数字,然后是较大的数字),但其工作以递归调用结束。

快速排序是一种排序算法,它以与中值算法完全相同的方式拆分数组。一旦通过两个递归调用对子数组进行了排序,就无事可做了。最坏的情况是,就像中位数查找一样。但是可以证明它的平均情况是; 此外,从经验上讲,它优于其他排序算法。这使quicksort在许多应用程序中倍受青睐-例如,它是对大量文件进行排序的代码基础。

资料来源:Dasgupta,Papadimitriou,Vazirani的算法。麦格劳-希尔。2006年

给定:正整数 和一个数组  来自的整数  至 。

返回:排序数组。

 

Sample Dataset

7
5 -2 4 7 8 -10 11

Sample Output

-10 -2 4 5 7 8 11
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值