本系列文章由 @YhL_Leo 出品,转载请注明出处。

文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069


快速排序算法,由C.A.R.Hoare于1962年提出,算法相当简单精炼,基本策略是随机分治。首先选取一个枢纽元(pivot),然后将数据划分成左右两部分,左边的大于(或等于)枢纽元,右边的小于(或等于枢纽元),最后递归处理左右两部分。分治算法一般分成三个部分:分解、解决以及合并。快排是就地排序,所以就不需要合并了。只需要划分(partition)和解决(递归)两个步骤。因为划分的结果决定递归的位置,所以Partition是整个算法的核心。快速排序最佳运行时间O(nlogn),最坏运行时间O(n2),随机化以后期望运行时间O(nlogn)。

首先来看一段升序快速排序算法的实现代码:

  1. #include <iostream>
  2. using namespace std;
  3. void quickSort(int arr[], int first, int last);
  4. void printArray(int arr[], const int& N);
  5. void main()
  6. {
  7. int test[] = { 1, 12, 5, 26, 7, 14, 3, 7, 2 };
  8. int N = sizeof(test)/sizeof(int);
  9. cout << "Size of test array :" << N << endl;
  10. cout << "Before sorting : " << endl;
  11. printArray(test, N);
  12. quickSort(test, 0, N-1);
  13. cout << endl << endl << "After sorting : " << endl;
  14. printArray(test, N);
  15. }
  16. /**
  17. * Quicksort.
  18. * @param a - The array to be sorted.
  19. * @param first - The start of the sequence to be sorted.
  20. * @param last - The end of the sequence to be sorted.
  21. */
  22. void quickSort(int arr[], int left, int right)
  23. {
  24. int i = left, j = right;
  25. int tmp;
  26. int pivot = arr[(left + right) / 2];
  27. /* partition */
  28. while (i <= j)
  29. {
  30. while (arr[i] < pivot)
  31. i++;
  32. while (arr[j] > pivot)
  33. j--;
  34. if (i <= j)
  35. {
  36. tmp = arr[i];
  37. arr[i] = arr[j];
  38. arr[j] = tmp;
  39. i++;
  40. j--;
  41. }
  42. }
  43. /* recursion */
  44. if (left < j)
  45. quickSort(arr, left, j);
  46. if (i < right)
  47. quickSort(arr, i, right);
  48. }
  49. /**
  50. * Print an array.
  51. * @param a - The array.
  52. * @param N - The size of the array.
  53. */
  54. void printArray(int arr[], const int& N)
  55. {
  56. for(int i = 0 ; i < N ; i++)
  57. cout << "array[" << i << "] = " << arr[i] << endl;
  58. }

1 划分(Partition)

划分分为两个步骤:

  • 选取枢纽元
  • 根据枢纽元所在位置将数组分为左右两部分

1.1 选取枢纽元

所谓的枢纽元,也就是将数组分为两部分的参考元素,选取的方式并不唯一。对于完全随机的数据,枢纽元的选取不是很重要,往往可以直接选取数组的初始位置的元素作为枢纽元。但是实际中,数据往往是部分有序的,如果仍然使用数组两端的数据作为枢纽元,划分的效果往往不好,导致运行时间退化为O(n2)。因此,这里给出的代码就是选取数组中间位置元素:

  1. int pivot = arr[(left + right) / 2];

也有三数取中的方法、随机选取法等。

1.2 根据枢纽元分为左右两部分

上文算法代码使用的是Hoara的双向扫描方法:

  1. /* partition */
  2. while (i <= j)
  3. {
  4. while (arr[i] < pivot)
  5. i++;
  6. while (arr[j] > pivot)
  7. j--;
  8. if (i <= j)
  9. {
  10. tmp = arr[i];
  11. arr[i] = arr[j];
  12. arr[j] = tmp;
  13. i++;
  14. j--;
  15. }
  16. }

除此以外还有单向扫描,双向扫描(区别于Hoara的方法)以及改进的双向扫描等。

1.3 关于双向扫描的思考

  • 内层循环中的while循环条件是用<=/>=还是</>

    • 一般的想法是用<=/>=,忽略与枢纽元相同的元素,这样可以减少不必要的交换,因为这些元素无论放在哪一边都是一样的。但是如果遇到所有元素都一样的情况,这种方法每次都会产生最坏的划分,也就是一边1个元素,令一边n−1个元素,使得时间复杂度变成O(n2)。而如果用严格</>,虽然两边指针每此只挪动1位,但是它们会在正中间相遇,产生一个最好的划分。
    • 也有人分析,认为内循环使用严格</>,可以减少内循环。
    • 因此,建议内循环使用</>
  • 小数组的特殊处理
    • 按照上面的方法,递归会持续到分区只有一个元素。而事实上,当分割到一定大小后,继续分割的效率比插入排序要差。由统计方法得到的数值是50左右,也有采用20的,这样quickSort函数就可以优化成:
  1. void newQuickSort(int arr[], int left, int right, int thresh)
  2. {
  3. if(right - left > thresh)
  4. {
  5. // quick sort for large array
  6. quickSort(arr, left, right);
  7. }
  8. else
  9. {
  10. // insertion sort for small array
  11. insertionSort(arr, left, right);
  12. }
  13. }

2 递归(Recursive)

即重复上述的划分(Partition)操作,最底层的情形是数列的大小是0或者1。快速排序算法和大多数分治排序方法一样,都有两次递归调用,但是快速排序的递归在函数尾部,因此可以实施尾递归优化,从而缩减堆栈的深度,减少算法的时间复杂度。

最后,贴上前文代码运行的过程:


参考文献

C/C++ Quick Sort Algorithm的更多相关文章

  1. Quick Sort Algorithm

    快速排序算法实现代码: //============================================================================ // Name : ...

  2. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  3. PAT1101:Quick Sort

    1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...

  4. A1101. Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  5. 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  6. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  7. PAT 1101 Quick Sort[一般上]

    1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...

  8. What does Quick Sort look like in Python?

    Let's talk about something funny at first. Have you ever implemented the Quick Sort algorithm all by ...

  9. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

随机推荐

  1. mybatis入门截图四(订单商品数据模型 一对一,一对多,多对多)

    --------------------------------- 一对一查询 查询订单信息,关联查询创建订单的用户信息 1.高级映射-一对一查询-使用resultType 2.高级映射-一对一查询- ...

  2. 使用githug游戏提高git水平

  3. Ubuntu12.04 下 GTK3.xx 的安装、编译和測试

    用此方法成功在UBUNTU 12.04下安装GTK 3.xxx. 一.安装 1.安装gcc/g++/gdb/make 等基本编程工具 $sudo apt-get install build-essen ...

  4. 一个关于Class的小点

    public 是公有 private 是私有 没有写就是private

  5. android继续探索Fresco

    我们接着上文继续说,上篇博客中我们已经知道了Fresco怎么用,也知道了它的非常多属性.可是非常多时候xml文件是不能满足你的要求的.这就须要你在代码中动态的改变显示的内容,今天我们就来探索一下怎样在 ...

  6. Agile实践日志一 -- Grooming Session

    Agile实践日志一  -- Grooming Session GroomingSession 这个Session主要Go through我们下一个Sprint须要做的Story,大家都清楚之后,在每 ...

  7. C4

    #include <stdio.h> int main(int argc, const char * argv[]) { // int 占用4个字节 double 占用8个字节 // 只是 ...

  8. Oracle Hint的用法

    1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_I ...

  9. 反向Shell增强

    下载socat 在客户端: socat file:`tty`,raw,echo=0 tcp-listen:4444 在服务端: socat exec:'bash -li',pty,stderr,set ...

  10. centos + nodejs + egg2.x 开发微信分享功能

    本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd