{"id":37,"date":"2026-04-05T16:01:57","date_gmt":"2026-04-05T08:01:57","guid":{"rendered":"https:\/\/blog.sjll.top\/?p=37"},"modified":"2026-04-05T16:01:57","modified_gmt":"2026-04-05T08:01:57","slug":"%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e8%af%a6%e8%a7%a3%ef%bc%9ac%e5%ae%9e%e7%8e%b0%e4%b8%8e%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/www.sjll.top\/?p=37","title":{"rendered":"\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u8be6\u89e3\uff1aC++\u5b9e\u73b0\u4e0e\u4f18\u5316\u6280\u5de7"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u5feb\u901f\u6392\u5e8f\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5feb\u901f\u6392\u5e8f\uff08Quick Sort\uff09<\/strong> \u662f\u7531\u82f1\u56fd\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6Tony Hoare\u4e8e1959\u5e74\u53d1\u660e\u7684\u4e00\u79cd\u9ad8\u6548\u7684\u5206\u6cbb\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u662f\u76ee\u524d\u6700\u5e38\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u4e4b\u4e00\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n log n)\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8868\u73b0\u4f18\u5f02\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u601d\u60f3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5feb\u901f\u6392\u5e8f\u91c7\u7528<strong>\u5206\u6cbb\uff08Divide and Conquer\uff09<\/strong>\u7b56\u7565\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u5206\u89e3\uff08Divide\uff09<\/strong>\uff1a\u4ece\u6570\u7ec4\u4e2d\u9009\u62e9\u4e00\u4e2a<strong>\u57fa\u51c6\u5143\u7d20\uff08pivot\uff09<\/strong>\uff0c\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u90e8\u5206\uff1a<\/li><\/ol>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5de6\u4fa7\uff1a\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u7684\u5143\u7d20<\/li><li>\u53f3\u4fa7\uff1a\u6240\u6709\u5927\u4e8e\u57fa\u51c6\u7684\u5143\u7d20<\/li><\/ul>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u9012\u5f52\uff08Conquer\uff09<\/strong>\uff1a\u5bf9\u5de6\u53f3\u4e24\u90e8\u5206\u5206\u522b\u9012\u5f52\u8fdb\u884c\u5feb\u901f\u6392\u5e8f<\/li><li><strong>\u5408\u5e76\uff08Combine\uff09<\/strong>\uff1a\u7531\u4e8e\u6392\u5e8f\u662f\u5c31\u5730\u8fdb\u884c\u7684\uff0c\u9012\u5f52\u7ed3\u675f\u65f6\u6570\u7ec4\u5df2\u7ecf\u6709\u5e8f<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">partition \u5212\u5206\u65b9\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5e38\u7528\u7684\u662f <strong>Lomuto \u5212\u5206<\/strong> \u548c <strong>Hoare \u5212\u5206<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\n\/\/ Lomuto \u5212\u5206\u65b9\u5f0f\nint partition_lomuto(vector&lt;int&gt;&amp; arr, int low, int high) {\n    int pivot = arr[high];  \/\/ \u9009\u62e9\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\n    int i = low - 1;\n    \n    for (int j = low; j &lt; high; j++) {\n        if (arr[j] &lt;= pivot) {\n            i++;\n            swap(arr[i], arr[j]);\n        }\n    }\n    swap(arr[i + 1], arr[high]);\n    return i + 1;\n}\n\n\/\/ Hoare \u5212\u5206\u65b9\u5f0f\uff08\u66f4\u9ad8\u6548\uff09\nint partition_hoare(vector&lt;int&gt;&amp; arr, int low, int high) {\n    int pivot = arr[low];\n    int i = low - 1, j = high + 1;\n    \n    while (true) {\n        do { i++; } while (arr[i] &lt; pivot);\n        do { j--; } while (arr[j] &gt; pivot);\n        if (i &gt;= j) return j;\n        swap(arr[i], arr[j]);\n    }\n}\n\n\/\/ \u5feb\u901f\u6392\u5e8f\u4e3b\u51fd\u6570\nvoid quick_sort(vector&lt;int&gt;&amp; arr, int low, int high) {\n    if (low &lt; high) {\n        int pi = partition_lomuto(arr, low, high);\n        \n        \/\/ \u9012\u5f52\u6392\u5e8f\u5de6\u53f3\u4e24\u90e8\u5206\n        quick_sort(arr, low, pi - 1);\n        quick_sort(arr, pi + 1, high);\n    }\n}\n\n\/\/ \u4f18\u5316\u7248\u672c\uff1a\u4e09\u6570\u53d6\u4e2d\u9009\u53d6\u57fa\u51c6\nint median_of_three(vector&lt;int&gt;&amp; arr, int low, int high) {\n    int mid = low + (high - low) \/ 2;\n    \n    if (arr[low] &gt; arr[mid]) swap(arr[low], arr[mid]);\n    if (arr[low] &gt; arr[high]) swap(arr[low], arr[high]);\n    if (arr[mid] &gt; arr[high]) swap(arr[mid], arr[high]);\n    \n    return mid;  \/\/ \u8fd4\u56de\u4e2d\u95f4\u503c\u7684\u7d22\u5f15\n}\n\nint main() {\n    vector&lt;int&gt; arr = {10, 7, 8, 9, 1, 5};\n    int n = arr.size();\n    \n    cout &lt;&lt; \"\u6392\u5e8f\u524d: \";\n    for (int x : arr) cout &lt;&lt; x &lt;&lt; \" \";\n    cout &lt;&lt; endl;\n    \n    quick_sort(arr, 0, n - 1);\n    \n    cout &lt;&lt; \"\u6392\u5e8f\u540e: \";\n    for (int x : arr) cout &lt;&lt; x &lt;&lt; \" \";\n    cout &lt;&lt; endl;\n    \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5feb\u901f\u6392\u5e8f\u7684\u4f18\u5316\u6280\u5de7<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u4e09\u6570\u53d6\u4e2d\u6cd5<\/strong>\uff1a\u9009\u62e9low\u3001mid\u3001high\u4e09\u4e2a\u4f4d\u7f6e\u7684\u4e2d\u503c\u4f5c\u4e3a\u57fa\u51c6\uff0c\u907f\u514d\u6700\u574f\u60c5\u51b5<\/li><li><strong>\u5c0f\u6570\u7ec4\u4f7f\u7528\u63d2\u5165\u6392\u5e8f<\/strong>\uff1a\u5f53\u6570\u7ec4\u957f\u5ea6\u5c0f\u4e8e10\u65f6\uff0c\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u6548<\/li><li><strong>\u5c3e\u9012\u5f52\u4f18\u5316<\/strong>\uff1a\u51cf\u5c11\u9012\u5f52\u6df1\u5ea6<\/li><li><strong>\u4e09\u8def\u5212\u5206<\/strong>\uff1a\u5904\u7406\u5927\u91cf\u91cd\u590d\u5143\u7d20\u7684\u6570\u7ec4<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u60c5\u51b5<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><th>\u7a7a\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td>\u6700\u597d<\/td><td>O(n log n)<\/td><td>O(log n)<\/td><\/tr><tr><td>\u5e73\u5747<\/td><td>O(n log n)<\/td><td>O(log n)<\/td><\/tr><tr><td>\u6700\u574f<\/td><td>O(n\u00b2)<\/td><td>O(n)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u574f\u60c5\u51b5\u53d1\u751f\u5728\u6570\u7ec4\u5df2\u7ecf\u6709\u5e8f\u6216\u5b8c\u5168\u9006\u5e8f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u4f18\u5316\u6280\u5de7\u907f\u514d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5feb\u901f\u6392\u5e8f vs \u5176\u4ed6\u6392\u5e8f<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u7279\u6027<\/th><th>\u5feb\u901f\u6392\u5e8f<\/th><th>\u5f52\u5e76\u6392\u5e8f<\/th><th>\u5806\u6392\u5e8f<\/th><\/tr><\/thead><tbody><tr><td>\u5e73\u5747\u65f6\u95f4<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><\/tr><tr><td>\u6700\u574f\u65f6\u95f4<\/td><td>O(n\u00b2)<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><\/tr><tr><td>\u7a7a\u95f4\u590d\u6742\u5ea6<\/td><td>O(log n)<\/td><td>O(n)<\/td><td>O(1)<\/td><\/tr><tr><td>\u7a33\u5b9a\u6027<\/td><td>\u274c \u4e0d\u7a33\u5b9a<\/td><td>\u2705 \u7a33\u5b9a<\/td><td>\u274c \u4e0d\u7a33\u5b9a<\/td><\/tr><tr><td>\u539f\u5730\u6392\u5e8f<\/td><td>\u2705 \u662f<\/td><td>\u274c \u5426<\/td><td>\u2705 \u662f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5feb\u901f\u6392\u5e8f\u4e4b\u6240\u4ee5\u5e7f\u6cdb\u5e94\u7528\uff0c\u662f\u56e0\u4e3a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5e73\u5747\u6027\u80fd\u4f18\u79c0<\/li><li>\u539f\u5730\u6392\u5e8f\uff0c\u7a7a\u95f4\u6548\u7387\u9ad8<\/li><li>\u7f13\u5b58\u547d\u4e2d\u7387\u9ad8<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1\u5feb\u901f\u6392\u5e8f\u662f\u7406\u89e3\u5206\u6cbb\u601d\u60f3\u548c\u7b97\u6cd5\u4f18\u5316\u7684\u7edd\u4f73\u4f8b\u5b50\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5feb\u901f\u6392\u5e8f\uff1f \u5feb\u901f\u6392\u5e8f\uff08Quick Sort\uff09 \u662f\u7531\u82f1\u56fd\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6Tony Hoare\u4e8e1959\u5e74\u53d1\u660e\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[34,33,37,36,38,35,32],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-3","tag-conquer","tag-divide","tag-int-high","tag-int-low","tag-int-pivot","tag-lomuto","tag-c"],"_links":{"self":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}