{"id":2912,"date":"2011-05-04T19:45:27","date_gmt":"2011-05-04T11:45:27","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2912"},"modified":"2011-05-05T12:05:28","modified_gmt":"2011-05-05T04:05:28","slug":"%e5%88%86%e6%b2%bb%e7%ae%97%e6%b3%95%e4%b9%8b%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2912","title":{"rendered":"\u5206\u6cbb\u7b97\u6cd5\u4e4b\u5feb\u901f\u6392\u5e8f"},"content":{"rendered":"<p>\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u4e5f\u662f\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u7684\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u57fa\u672c\u64cd\u4f5c\u5373\u4e3a\u6bd4\u8f83-\u4ea4\u6362\u3002<\/p>\n<p>\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u4ece\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\u4e2d\u9009\u53d6\u4e00\u4e2a\u6bd4\u8f83\u6807\u51c6K\uff08\u901a\u5e38\u9009\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f59\u5143\u7d20\u4f9d\u6b21\u8ddfK\u8fdb\u884c\u6bd4\u8f83\u3002\u5728\u6bd4\u8f83\u7684\u8fc7\u7a0b\u4e2d\u5c06\u5927\u4e8eK\u7684\u5143\u7d20\u79fb\u5230K\u7684\u540e\u9762\uff0c\u5c06\u5c0f\u4e8eK\u7684\u5143\u7d20\u79fb\u5230K\u7684\u524d\u9762\uff0c\u6700\u540e\u7684\u7ed3\u679c\u662f\u5c06\u539f\u59cb\u5e8f\u5217\u5206\u4e3a\u4e24\u4e2a\u5b50\u5e8f\u5217\uff0c\u800cK\u5143\u7d20\u5219\u6070\u597d\u4f4d\u4e8e\u4e24\u4e2a\u5b50\u5217\u4e2d\u95f4\u3002\u4e0a\u8ff0\u8fc7\u7a0b\u79f0\u4e3a\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\uff0c\u63a5\u4e0b\u6765\u4f9d\u6b21\u4e3a\u4e24\u4e2a\u5b50\u5e8f\u5217\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u4f9d\u6b21\u9012\u5f52\u3002\u5f53\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u5c0f\u4e8e1\u65f6\uff0c\u9012\u5f52\u505c\u6b62\u3002\u6b64\u65f6\uff0c\u539f\u59cb\u5e8f\u5217\u5df2\u7ecf\u6210\u4e3a\u4e00\u4e2a\u6709\u5e8f\u7684\u5e8f\u5217\u4e86\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u601d\u60f3\uff0c\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\u6240\u793a\u3002quickSort\u51fd\u6570\u5bf9\u539f\u59cb\u5e8f\u5217\u9012\u5f52\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u6bcf\u6b21\u6392\u5e8f\u65f6\u5148\u901a\u8fc7partiton\u51fd\u6570\u5f97\u5230\u5e8f\u5217\u4e2dp\u5230r\u5143\u7d20\u95f4\u7684\u5206\u754c\u70b9q\uff0c\u7136\u540e\u518d\u5206\u522b\u5bf9\u4e24\u4e2a\u5b50\u5e8f\u5217p\u5230q-1\u548cq+1\u5230r\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\u3002<\/p>\n<pre class=\"brush:c\">void quickSort(int *a, int p, int r)\r\n{\r\n\tif (p &lt; r) {\r\n\t\tint q = partition(a, p, r);\r\n\t\tquickSort(a, p, q - 1);\r\n\t\tquickSort(a, q + 1, r);\r\n\t}\r\n}<\/pre>\n<p>partition\u51fd\u6570\u662f\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u5173\u952e\u3002\u8be5\u51fd\u6570\u9009\u53d6\u5f85\u6392\u5e8f\u5e8f\u5217\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\uff0c\u901a\u8fc7\u53cd\u590d\u7684\u6bd4\u8f83-\u4ea4\u6362\u5c06p\u5230r\u4e4b\u95f4\u7684\u5143\u7d20\u5206\u6210\u4e24\u7ec4\u5b50\u5e8f\u5217\uff0c\u4e00\u7ec4\u5b50\u5e8f\u5217\u7684\u5143\u7d20\u5168\u90e8\u5c0f\u4e8ex\uff0c\u53e6\u4e00\u7ec4\u5b50\u5e8f\u5217\u7684\u5143\u7d20\u5168\u90e8\u5927\u4e8ex\u3002<\/p>\n<p>\u5728\u5177\u4f53\u7684\u6bd4\u8f83-\u4ea4\u6362\u8fc7\u7a0b\u4e2d\uff0c\u8bbe\u7f6e\u4e24\u4e2a\u8bb0\u5f55\u70b9low\u548chigh\uff0c\u5e76\u5728\u521d\u59cb\u65f6\u5c06\u57fa\u51c6\u4fdd\u5b58\u5230x\u4e2d\u3002\u7136\u540e\u4e0d\u65ad\u8fdb\u884c\u4e0b\u9762\u4e24\u79cd\u626b\u63cf\uff1a<\/p>\n<p>1.\u5c06high\u4ece\u53f3\u81f3\u5de6\u626b\u63cf\uff0c\u76f4\u5230a[high] &lt; x\u4e3a\u6b62\uff0c\u7531\u4e8e\u6b64\u65f6\u7684a[high]\u662f\u7b2c\u4e00\u4e2a\u5c0f\u4e8e\u57fa\u51c6x\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u5c06a[high]\u548cx\u4ea4\u6362\u3002<\/p>\n<p>2.\u5c06low\u4ece\u5de6\u81f3\u53f3\u626b\u63cf\uff0c\u76f4\u5230a[low] &gt;= x\u4e3a\u6b62\uff0c\u7531\u4e8e\u6b64\u65f6\u7684a[low]\u662f\u7b2c\u4e00\u4e2a\u4e0d\u5c0f\u4e8e\u57fa\u51c6x\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u5c06a[low]\u548cx\u4ea4\u6362\u3002<\/p>\n<p>\u5f53low\u5c0f\u4e8ehigh\u65f6\u4f1a\u4e00\u76f4\u6301\u7eed\u4e0a\u8ff0\u4e24\u79cd\u626b\u63cf\uff0c\u5426\u5219\u79f0\u5176\u5b8c\u6210\u4e86\u4e00\u6b21\u5212\u5206\u8fc7\u7a0b\u3002\u6bcf\u4e00\u6b21\u7684\u5212\u5206\u8fc7\u7a0b\u5c31\u4f1a\u5f97\u5230\u5206\u754c\u4f4d\u7f6e\uff0c\u8fd4\u56de\u4e3aquickSort\u51fd\u6570\u3002<\/p>\n<pre class=\"brush:c\">int partition(int *a, int p, int r)\r\n{\r\n\tint x, low, high;\r\n\r\n\tx = a[p];\r\n\tlow = p;\r\n\thigh = r;\r\n\r\n\twhile (low &lt; high) {\r\n\t\twhile (low &lt; high &amp;&amp; a[high] &gt;= x)\r\n\t\t\thigh--;\r\n\t\tif (low &lt; high) {\r\n\t\t\ta[low] = a[high];\r\n\t\t\ta[high] = x;\r\n\t\t\tlow++;\r\n\t\t}\r\n\t\toutput_data(a, n);\r\n\r\n\t\twhile (low &lt; high &amp;&amp; a[low] &lt; x)\r\n\t\t\tlow++;\r\n\t\tif (low &lt; high) {\r\n\t\t\ta[high] = a[low];\r\n\t\t\ta[low] = x;\r\n\t\t\thigh--;\r\n\t\t}\r\n\t\toutput_data(a, n);\r\n\t}\r\n\ta[low] = x;\r\n\treturn low;\r\n}<\/pre>\n<p>\u5728partition\u51fd\u6570\u4e2d\uff0c\u9009\u62e9\u7b2c\u4e00\u4e2a\u5143\u7d20p\u4f5c\u4e3a\u57fa\u51c6\u53ef\u4ee5\u4fdd\u8bc1\u8be5\u51fd\u6570\u6b63\u5e38\u9000\u51fa\u3002\u5982\u679c\u9009\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20r\u4f5c\u4e3a\u57fa\u51c6\uff0c\u800c\u8be5\u5143\u7d20\u53c8\u6070\u597d\u662f\u6700\u5927\u5143\u7d20\uff0c\u90a3\u4e48partition\u51fd\u6570\u5c31\u4f1a\u8fd4\u56der\uff0c\u8fd9\u4f7f\u5f97quickSort\u65e0\u9650\u9012\u5f52\u4e0b\u53bb\u3002\u5b8c\u6574\u7684\u4ee3\u7801\u53ef\u5728\u8fd9\u91cc<a href=\"http:\/\/edsionte.download.csdn.net\/\" target=\"_blank\">\u4e0b\u8f7d<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u4e5f\u662f\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u7684\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u57fa\u672c\u64cd\u4f5c\u5373\u4e3a\u6bd4\u8f83-\u4ea4\u6362\u3002 \u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u4ece\u5f85\u6392\u5e8f\u7684\u5e8f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[215],"tags":[109,216,218,219],"class_list":["post-2912","post","type-post","status-publish","format-standard","hentry","category-215","tag-c","tag-216","tag-218","tag-219"],"views":6133,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=2912"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2912\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2912"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}