{"id":38406,"date":"2023-12-11T14:44:22","date_gmt":"2023-12-11T06:44:22","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=38406"},"modified":"2023-12-11T14:44:22","modified_gmt":"2023-12-11T06:44:22","slug":"%e8%af%b7%e4%bd%a0%e8%af%b4%e4%b8%80%e8%af%b4%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f%ef%bc%8c%e5%b9%b6%e6%89%8b%e5%86%99%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e8%af%b7%e4%bd%a0%e8%af%b4%e4%b8%80%e8%af%b4%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f%ef%bc%8c%e5%b9%b6%e6%89%8b%e5%86%99%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u8bf7\u4f60\u8bf4\u4e00\u8bf4\u5feb\u901f\u6392\u5e8f\uff0c\u5e76\u624b\u5199\u4ee3\u7801"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u53c2\u8003\u56de\u7b54\uff1a<\/p>\n<p>1\u3001\u5feb\u901f\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\uff1a<\/p>\n<p>\u5feb\u901f\u6392\u5e8f\u4f7f\u7528\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u901a\u8fc7\u4e00\u8d9f\u6392\u5e8f\u5c06\u5f85\u6392\u5e8f\u5217\u5206\u5272\u6210\u4e24\u90e8\u5206\uff0c\u5176\u4e2d\u4e00\u90e8\u5206\u8bb0\u5f55\u7684\u5173\u952e\u5b57\u5747\u6bd4\u53e6\u4e00\u90e8\u5206\u8bb0\u5f55\u7684\u5173\u952e\u5b57\u5c0f\u3002\u4e4b\u540e\u5206\u522b\u5bf9\u8fd9\u4e24\u90e8\u5206\u8bb0\u5f55\u7ee7\u7eed\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u8fbe\u5230\u6574\u4e2a\u5e8f\u5217\u6709\u5e8f\u7684\u76ee\u7684\u3002<\/p>\n<p>2\u3001\u5feb\u901f\u6392\u5e8f\u7684\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<p>(1)\u9009\u62e9\u57fa\u51c6\uff1a\u5728\u5f85\u6392\u5e8f\u5217\u4e2d\uff0c\u6309\u7167\u67d0\u79cd\u65b9\u5f0f\u6311\u51fa\u4e00\u4e2a\u5143\u7d20\uff0c\u4f5c\u4e3a \u201c\u57fa\u51c6\u201d\uff08pivot\uff09<\/p>\n<p>(2)\u5206\u5272\u64cd\u4f5c\uff1a\u4ee5\u8be5\u57fa\u51c6\u5728\u5e8f\u5217\u4e2d\u7684\u5b9e\u9645\u4f4d\u7f6e\uff0c\u628a\u5e8f\u5217\u5206\u6210\u4e24\u4e2a\u5b50\u5e8f\u5217\u3002\u6b64\u65f6\uff0c\u5728\u57fa\u51c6\u5de6\u8fb9\u7684\u5143\u7d20\u90fd\u6bd4\u8be5\u57fa\u51c6\u5c0f\uff0c\u5728\u57fa\u51c6\u53f3\u8fb9\u7684\u5143\u7d20\u90fd\u6bd4\u57fa\u51c6\u5927<\/p>\n<p>(3)\u9012\u5f52\u5730\u5bf9\u4e24\u4e2a\u5e8f\u5217\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u76f4\u5230\u5e8f\u5217\u4e3a\u7a7a\u6216\u8005\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>3\u3001\u9009\u62e9\u57fa\u51c6\u7684\u65b9\u5f0f\uff1a<\/p>\n<p>\u5bf9\u4e8e\u5206\u6cbb\u7b97\u6cd5\uff0c\u5f53\u6bcf\u6b21\u5212\u5206\u65f6\uff0c\u7b97\u6cd5\u82e5\u90fd\u80fd\u5206\u6210\u4e24\u4e2a\u7b49\u957f\u7684\u5b50\u5e8f\u5217\u65f6\uff0c\u90a3\u4e48\u5206\u6cbb\u7b97\u6cd5\u6548\u7387\u4f1a\u8fbe\u5230\u6700\u5927\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u57fa\u51c6\u7684\u9009\u62e9\u662f\u5f88\u91cd\u8981\u7684\u3002\u9009\u62e9\u57fa\u51c6\u7684\u65b9\u5f0f\u51b3\u5b9a\u4e86\u4e24\u4e2a\u5206\u5272\u540e\u4e24\u4e2a\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\uff0c\u8fdb\u800c\u5bf9\u6574\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u4ea7\u751f\u51b3\u5b9a\u6027\u5f71\u54cd\u3002\u6700\u7406\u60f3\u7684\u65b9\u6cd5\u662f\uff0c\u9009\u62e9\u7684\u57fa\u51c6\u6070\u597d\u80fd\u628a\u5f85\u6392\u5e8f\u5e8f\u5217\u5206\u6210\u4e24\u4e2a\u7b49\u957f\u7684\u5b50\u5e8f\u5217\u4e09\u79cd\u9009\u62e9\u57fa\u51c6\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>\u65b9\u6cd5(1)\uff1a\u56fa\u5b9a\u4f4d\u7f6e<\/p>\n<p>\u601d\u60f3\uff1a\u53d6\u5e8f\u5217\u7684\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6<\/p>\n<p>\u65b9\u6cd5(2)\uff1a\u968f\u673a\u9009\u53d6\u57fa\u51c6<\/p>\n<p>\u5f15\u5165\u7684\u539f\u56e0\uff1a\u5728\u5f85\u6392\u5e8f\u5217\u662f\u90e8\u5206\u6709\u5e8f\u65f6\uff0c\u56fa\u5b9a\u9009\u53d6\u67a2\u8f74\u4f7f\u5feb\u6392\u6548\u7387\u5e95\u4e0b\uff0c\u8981\u7f13\u89e3\u8fd9\u79cd\u60c5\u51b5\uff0c\u5c31\u5f15\u5165\u4e86\u968f\u673a\u9009\u53d6\u67a2\u8f74<\/p>\n<p>\u601d\u60f3\uff1a\u53d6\u5f85\u6392\u5e8f\u5217\u4e2d\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6<\/p>\n<p>\u65b9\u6cd5(3)\uff1a\u4e09\u6570\u53d6\u4e2d\uff08median-of-three\uff09<\/p>\n<p>\u5f15\u5165\u7684\u539f\u56e0\uff1a\u867d\u7136\u968f\u673a\u9009\u53d6\u67a2\u8f74\u65f6\uff0c\u51cf\u5c11\u51fa\u73b0\u4e0d\u597d\u5206\u5272\u7684\u51e0\u7387\uff0c\u4f46\u662f\u8fd8\u662f\u6700\u574f\u60c5\u51b5\u4e0b\u8fd8\u662fO(n^2\uff09\uff0c\u8981\u7f13\u89e3\u8fd9\u79cd\u60c5\u51b5\uff0c\u5c31\u5f15\u5165\u4e86\u4e09\u6570\u53d6\u4e2d\u9009\u53d6\u67a2\u8f74<\/p>\n<p>\u5206\u6790\uff1a\u6700\u4f73\u7684\u5212\u5206\u662f\u5c06\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\u5206\u6210\u7b49\u957f\u7684\u5b50\u5e8f\u5217\uff0c\u6700\u4f73\u7684\u72b6\u6001\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e8f\u5217\u7684\u4e2d\u95f4\u7684\u503c\uff0c\u4e5f\u5c31\u662f\u7b2cN\/2\u4e2a\u6570\u3002\u53ef\u662f\uff0c\u8fd9\u5f88\u96be\u7b97\u51fa\u6765\uff0c\u5e76\u4e14\u4f1a\u660e\u663e\u51cf\u6162\u5feb\u901f\u6392\u5e8f\u7684\u901f\u5ea6\u3002\u8fd9\u6837\u7684\u4e2d\u503c\u7684\u4f30\u8ba1\u53ef\u4ee5\u901a\u8fc7\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u5e76\u7528\u5b83\u4eec\u7684\u4e2d\u503c\u4f5c\u4e3a\u67a2\u7ebd\u5143\u800c\u5f97\u5230\u3002\u4e8b\u5b9e\u4e0a\uff0c\u968f\u673a\u6027\u5e76\u6ca1\u6709\u591a\u5927\u7684\u5e2e\u52a9\uff0c\u56e0\u6b64\u4e00\u822c\u7684\u505a\u6cd5\u662f\u4f7f\u7528\u5de6\u7aef\u3001\u53f3\u7aef\u548c\u4e2d\u5fc3\u4f4d\u7f6e\u4e0a\u7684\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u503c\u4f5c\u4e3a\u67a2\u7ebd\u5143\u3002\u663e\u7136\u4f7f\u7528\u4e09\u6570\u4e2d\u503c\u5206\u5272\u6cd5\u6d88\u9664\u4e86\u9884\u6392\u5e8f\u8f93\u5165\u7684\u4e0d\u597d\u60c5\u5f62\uff0c\u5e76\u4e14\u51cf\u5c11\u5feb\u6392\u5927\u7ea614%\u7684\u6bd4\u8f83\u6b21\u6570\u3002<\/p>\n<p>\u56fa\u5b9a\u4f4d\u7f6e\u793a\u4f8b\u7a0b\u5e8f\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public class QuickSort {<br \/>\npublic static void main(String[] args) {<br \/>\nint[] a = {1, 2, 4, 5, 7, 4, 5 ,3 ,9 ,0};<br \/>\nSystem.out.println(Arrays.toString(a));<br \/>\nquickSort(a);<br \/>\nSystem.out.println(Arrays.toString(a));<br \/>\n}<br \/>\npublic static void quickSort(int[] a) {<br \/>\nif(a.length&gt;0) {<br \/>\nquickSort(a, 0 , a.length-1);<br \/>\n}<br \/>\n}<br \/>\nprivate static void quickSort(int[] a, int low, int high) {<br \/>\n    \/\/1,\u627e\u5230\u9012\u5f52\u7b97\u6cd5\u7684\u51fa\u53e3<\/p>\n<p>    if( low &gt; high) {<br \/>\n    return;<br \/>\n    }<br \/>\n\/\/2, \u5b58<br \/>\nint i = low;<br \/>\nint j = high;<br \/>\n\/\/3,key<br \/>\nint key = a[ low ];<br \/>\n\/\/4\uff0c\u5b8c\u6210\u4e00\u8d9f\u6392\u5e8f<\/p>\n<p>while( i&lt; j) {<\/p>\n<p>\/\/4.1 \uff0c\u4ece\u53f3\u5f80\u5de6\u627e\u5230\u7b2c\u4e00\u4e2a\u5c0f\u4e8ekey\u7684\u6570<br \/>\nwhile(i&lt;j &amp;&amp; a[j] &gt; key){<br \/>\nj&#8211;;<br \/>\n}<br \/>\n\/\/ 4.2 \u4ece\u5de6\u5f80\u53f3\u627e\u5230\u7b2c\u4e00\u4e2a\u5927\u4e8ekey\u7684\u6570<\/p>\n<p>    while( i&lt;j &amp;&amp; a[i] &lt;= key) {<br \/>\n    i++;<br \/>\n    }<br \/>\n\/\/4.3 \u4ea4\u6362<\/p>\n<p>    if(i&lt;j) {<br \/>\n    int p = a[i];<br \/>\n    a[i] = a[j];<br \/>\n    a[j] = p;<br \/>\n    }<br \/>\n    }<br \/>\n\/\/ 4.4\uff0c\u8c03\u6574key\u7684\u4f4d\u7f6e<\/p>\n<p>int p = a[i];<\/p>\n<p>a[i] = a[low];<\/p>\n<p>a[low] = p;<\/p>\n<p>\/\/5, \u5bf9key\u5de6\u8fb9\u7684\u6570\u5feb\u6392<\/p>\n<p>quickSort(a, low, i-1 );<\/p>\n<p>\/\/6, \u5bf9key\u53f3\u8fb9\u7684\u6570\u5feb\u6392<\/p>\n<p>quickSort(a, i+1, high);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u56db\u79cd\u4f18\u5316\u65b9\u5f0f\uff1a<\/p>\n<p>\u4f18\u53161\uff1a\u5f53\u5f85\u6392\u5e8f\u5e8f\u5217\u7684\u957f\u5ea6\u5206\u5272\u5230\u4e00\u5b9a\u5927\u5c0f\u540e\uff0c\u4f7f\u7528\u63d2\u5165\u6392\u5e8f<\/p>\n<p>\u539f\u56e0\uff1a\u5bf9\u4e8e\u5f88\u5c0f\u548c\u90e8\u5206\u6709\u5e8f\u7684\u6570\u7ec4\uff0c\u5feb\u6392\u4e0d\u5982\u63d2\u6392\u597d\u3002\u5f53\u5f85\u6392\u5e8f\u5e8f\u5217\u7684\u957f\u5ea6\u5206\u5272\u5230\u4e00\u5b9a\u5927\u5c0f\u540e\uff0c\u7ee7\u7eed\u5206\u5272\u7684\u6548\u7387\u6bd4\u63d2\u5165\u6392\u5e8f\u8981\u5dee\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u63d2\u6392\u800c\u4e0d\u662f\u5feb\u6392<\/p>\n<p>\u4f18\u53162\uff1a\u5728\u4e00\u6b21\u5206\u5272\u7ed3\u675f\u540e\uff0c\u53ef\u4ee5\u628a\u4e0eKey\u76f8\u7b49\u7684\u5143\u7d20\u805a\u5728\u4e00\u8d77\uff0c\u7ee7\u7eed\u4e0b\u6b21\u5206\u5272\u65f6\uff0c\u4e0d\u7528\u518d\u5bf9\u4e0ekey\u76f8\u7b49\u5143\u7d20\u5206\u5272<\/p>\n<p>\u4f18\u53163\uff1a\u4f18\u5316\u9012\u5f52\u64cd\u4f5c<\/p>\n<p>\u5feb\u6392\u51fd\u6570\u5728\u51fd\u6570\u5c3e\u90e8\u6709\u4e24\u6b21\u9012\u5f52\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u4f7f\u7528\u5c3e\u9012\u5f52\u4f18\u5316<\/p>\n<p>\u4f18\u70b9\uff1a\u5982\u679c\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\u5212\u5206\u6781\u7aef\u4e0d\u5e73\u8861\uff0c\u9012\u5f52\u7684\u6df1\u5ea6\u5c06\u8d8b\u8fd1\u4e8en\uff0c\u800c\u6808\u7684\u5927\u5c0f\u662f\u5f88\u6709\u9650\u7684\uff0c\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u90fd\u4f1a\u8017\u8d39\u4e00\u5b9a\u7684\u6808\u7a7a\u95f4\uff0c\u51fd\u6570\u7684\u53c2\u6570\u8d8a\u591a\uff0c\u6bcf\u6b21\u9012\u5f52\u8017\u8d39\u7684\u7a7a\u95f4\u4e5f\u8d8a\u591a\u3002\u4f18\u5316\u540e\uff0c\u53ef\u4ee5\u7f29\u51cf\u5806\u6808\u6df1\u5ea6\uff0c\u7531\u539f\u6765\u7684O(n)\u7f29\u51cf\u4e3aO(logn)\uff0c\u5c06\u4f1a\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>\u4f18\u53164\uff1a\u4f7f\u7528\u5e76\u884c\u6216\u591a\u7ebf\u7a0b\u5904\u7406\u5b50\u5e8f\u5217<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u53c2\u8003\u56de\u7b54\uff1a 1\u3001\u5feb\u901f\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\uff1a \u5feb\u901f\u6392\u5e8f\u4f7f\u7528\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u901a\u8fc7\u4e00\u8d9f\u6392\u5e8f\u5c06\u5f85 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-38406","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/38406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=38406"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/38406\/revisions"}],"predecessor-version":[{"id":38407,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/38406\/revisions\/38407"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=38406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=38406"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=38406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}