剑指offer之快速排序

举报
chenyu 发表于 2021/07/27 00:10:46 2021/07/27
【摘要】 1 快速排序 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列       2 分析思路 很明显,先是用到了 partition算法思想(前面的博客提到了),然后再把原始数据分...

1 快速排序

通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列

 

 

 

2 分析思路

很明显,先是用到了 partition算法思想(前面的博客提到了),然后再把原始数据分成几部分然后递归同样用partition算法处理

 

 

 

3 代码实现

1) 代码实现方式1


  
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. /*
  5. *交换函数
  6. */
  7. void swap(int* a, int* b)
  8. {
  9. int temp = *a;
  10. *a = *b;
  11. *b = temp;
  12. }
  13. /*
  14. * 打印vector
  15. */
  16. void printVector(vector<int> v)
  17. {
  18. for (int i = 0; i < v.size(); ++i)
  19. {
  20. std::cout << v[i] << "\t";
  21. }
  22. std::cout << std::endl;
  23. }
  24. /*
  25. *partition算法 记得如果这里是C++我们传递的是vector类型,我们记得要加引用,
  26. *不然改变不了数据,这里和java传递ArrayList不一样,ArrayList作为参数可以改变集合里面的值,
  27. *所以C++如果函数传递非基本数据类型,一半都是带引用的
  28. */
  29. int partitionOne(vector<int>& vector, int start, int end)
  30. {
  31. if (start > end)
  32. {
  33. std::cout << "vector is empty or start > end" << std::endl;
  34. return -1;
  35. }
  36. int pivot = vector[start];
  37. while (start < end)
  38. {
  39. //我们先从尾巴开始
  40. while (start < end && pivot <= vector[end])
  41. {
  42. --end;
  43. }
  44. //这里用的数组赋值,而不是直接用swap交换函数,那么下面的2步也是用数组赋值,而不是用swap交换函数
  45. vector[start] = vector[end];
  46. while (start < end && pivot >= vector[start])
  47. {
  48. ++start;
  49. }
  50. vector[end] = vector[start];
  51. }
  52. vector[start] = pivot;
  53. printVector(vector);
  54. return start;
  55. }
  56. /*
  57. *partition算法, 这里只不过增加了2个变量i和j
  58. *,
  59. */
  60. int partitionTwo(vector<int>& vector, int start, int end)
  61. {
  62. if (start > end)
  63. {
  64. return -1;
  65. }
  66. int i = start;
  67. int j = end;
  68. int pivot = vector[start];
  69. while (i < j)
  70. {
  71. //我们先从尾巴开始
  72. while (i < j && pivot <= vector[j])
  73. {
  74. --j;
  75. }
  76. //这里用的数组赋值,而不是直接用swap交换函数,那么下面的2步也是用数组赋值,而不是用swap交换函数
  77. vector[i] = vector[j];
  78. while (i < j && pivot >= vector[i])
  79. {
  80. ++i;
  81. }
  82. vector[j] = vector[i];
  83. }
  84. vector[i] = pivot;
  85. printVector(vector);
  86. // quickSort1(vector, start, i - 1);/*最后用同样的方式对分出来的左边的小组进行同上的做法*/
  87. // quickSort1(vector, i + 1, end);
  88. return i;
  89. }
  90. /*
  91. *partition算法, 这里只不过增加了2个变量i和j,然后使用了交换函数swap
  92. *,
  93. */
  94. int partitionThree(vector<int>& vector, int start, int end)
  95. {
  96. if (start > end)
  97. {
  98. return -1;
  99. }
  100. int i = start;
  101. int j = end;
  102. int pivot = vector[start];
  103. while (i < j)
  104. {
  105. //我们先从尾巴开始
  106. while (i < j && pivot <= vector[j])
  107. {
  108. --j;
  109. }
  110. while (i < j && pivot >= vector[i])
  111. {
  112. ++i;
  113. }
  114. //这里用的shiswap交换函数,那么下面的是是也是swap交换函数
  115. swap(vector[i], vector[j]);
  116. }
  117. swap(vector[i], vector[start]);
  118. printVector(vector);
  119. return i;
  120. }
  121. /**
  122. *快速排序 调用第一个partitionOne
  123. */
  124. void quickSortOne(vector<int>& vector, int start, int end)
  125. {
  126. if (vector.size() < 0 || start > end)
  127. return;
  128. int index = partitionOne(vector, start, end);
  129. quickSortOne(vector, start, index - 1);
  130. quickSortOne(vector, index + 1, end);
  131. }
  132. /**
  133. *快速排序 调用第二个partitionTwo
  134. */
  135. void quickSortTwo(vector<int>& vector, int start, int end)
  136. {
  137. if (vector.size() < 0 || start > end)
  138. return;
  139. int index = partitionTwo(vector, start, end);
  140. quickSortTwo(vector, start, index - 1);
  141. quickSortTwo(vector, index + 1, end);
  142. }
  143. /**
  144. *快速排序 调用第三个partitionThree
  145. */
  146. void quickSortThree(vector<int>& vector, int start, int end)
  147. {
  148. if (vector.size() < 0 || start > end)
  149. return;
  150. int index = partitionThree(vector, start, end);
  151. quickSortThree(vector, start, index - 1);
  152. quickSortThree(vector, index + 1, end);
  153. }
  154. int main()
  155. {
  156. vector<int> v1;
  157. //[5,9,2,1,4,7,5,8,3,6]
  158. v1.push_back(5);
  159. v1.push_back(9);
  160. v1.push_back(2);
  161. v1.push_back(1);
  162. v1.push_back(4);
  163. v1.push_back(7);
  164. v1.push_back(5);
  165. v1.push_back(8);
  166. v1.push_back(3);
  167. v1.push_back(6);
  168. std::cout << "old data print " << std::endl;
  169. printVector(v1);
  170. quickSortOne(v1, 0, v1.size() - 1);
  171. // quickSortTwo(v1, 0, v1.size() - 1);
  172. // quickSortThree(v1, 0, v1.size() - 1);
  173. std::cout << "after partitionOne" << std::endl;
  174. printVector(v1);
  175. return 0;
  176. }

运行结果如下


  
  1. old data print
  2. 5 9 2 1 4 7 5 8 3 6
  3. 3 4 2 1 5 7 5 8 9 6
  4. 1 2 3 4 5 7 5 8 9 6
  5. 1 2 3 4 5 7 5 8 9 6
  6. 1 2 3 4 5 7 5 8 9 6
  7. 1 2 3 4 5 7 5 8 9 6
  8. 1 2 3 4 5 6 5 7 9 8
  9. 1 2 3 4 5 5 6 7 9 8
  10. 1 2 3 4 5 5 6 7 9 8
  11. 1 2 3 4 5 5 6 7 8 9
  12. 1 2 3 4 5 5 6 7 8 9
  13. after partitionOne
  14. 1 2 3 4 5 5 6 7 8 9

上面我们写了3个parition函数,我们调用quickSortOne(v1, 0, v1.size() - 1)或者quickSortTwo(v1, 0, v1.size() - 1)或者quickSortThree(v1, 0, v1.size() - 1)其中的的一个,结果都是一样。

 

 

 

 

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/102634525

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。