{"id":3812,"date":"2012-03-16T17:06:55","date_gmt":"2012-03-16T09:06:55","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3812"},"modified":"2012-03-30T21:55:51","modified_gmt":"2012-03-30T13:55:51","slug":"%e4%ba%a4%e6%8d%a2%e7%b1%bb%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3812","title":{"rendered":"\u4ea4\u6362\u7c7b\u6392\u5e8f"},"content":{"rendered":"<p>\u4ea4\u6362\u7c7b\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u4ea4\u6362\u9006\u5e8f\u5143\u7d20\u800c\u6700\u7ec8\u8fbe\u5230\u6240\u6709\u5143\u7d20\u6709\u5e8f\uff0c\u8fd9\u91cc\u7684\u9006\u5e8f\u662f\u4e2a\u5e7f\u4e49\u6982\u5ff5\uff0c\u5982\u679c\u6309\u7167\u964d\u5e8f\u6392\u5e8f\uff0c\u90a3\u4e48\u524d\u5c0f\u540e\u5927\u7684\u76f8\u90bb\u5143\u7d20\u5c31\u4e3a\u9006\u5e8f\u3002\u5e38\u89c1\u7684\u4ea4\u6362\u7c7b\u6392\u5e8f\u65b9\u6cd5\u6709\u5192\u6ce1\u6392\u5e8f\u548c\u5feb\u901f\u6392\u5e8f\u3002<\/p>\n<h3>1.\u5192\u6ce1\u6392\u5e8f<\/h3>\n<p>\u5192\u6ce1\u6392\u5e8f\u6cd5\u7684\u601d\u60f3\u6bd4\u8f83\u7b80\u5355\uff0c\u4f9d\u6b21\u626b\u63cf\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\uff0c\u5e76\u4e14\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u6bd4\u8f83\u76f8\u90bb\u4e24\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u5927\u5c0f\uff0c\u5982\u679c\u9006\u5e8f\u5219\u4ea4\u6362\u3002\u5047\u5982\u6709\u4e00\u4e2a\u8bb0\u5f55\u5e8f\u5217r[1,length]\uff0c\u4ee5\u5347\u5e8f\u4e3a\u4f8b\uff0c\u5728\u7b2ci\u8d9f\u6392\u5e8f\u8fc7\u7a0b\u4e2d\u9700\u8981\u5bf9\u524dlength-(i-1)\u4e2a\u5143\u7d20\u8fdb\u884c\u9006\u5e8f\u4ea4\u6362\uff0c\u6700\u7ec8\u8fd9\u4e9b\u8bb0\u5f55\u4e2d\u7684\u6700\u5927\u5143\u7d20\u5c06\u88ab\u4ea4\u6362\u5230\u5e8f\u5217\u4e2d\u7b2clength-(i-1)\u8fd9\u4e2a\u4f4d\u7f6e\u4e0a\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid bubbleSort(int *r, int length)\r\n{\r\n\tint n, change;\r\n\tint i, j, k;\r\n\r\n\tn = length;\r\n\tchange = 1;\r\n\r\n\tfor ( i = 1; i &lt;= n - 1 &amp;&amp; change; i++) {\r\n\t\tchange = 0;\r\n\t\tfor ( j = 1; j &lt;= n - i; j++)\r\n\t\t\tif (r&#x5B;j] &gt; r&#x5B;j + 1]) {\r\n\t\t\t\tr&#x5B;0] = r&#x5B;j];\r\n\t\t\t\tr&#x5B;j] = r&#x5B;j + 1];\r\n\t\t\t\tr&#x5B;j + 1] = r&#x5B;0];\r\n\t\t\t\tchange = 1;\r\n\t\t\t}\r\n\t\toutput(r, length);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u5bf9\u4e8en\u4e2a\u5143\u7d20\u7684\u5e8f\u5217\u6765\u8bf4\uff0c\u6574\u4e2a\u5192\u6ce1\u6392\u5e8f\u6700\u591a\u8fdb\u884cn-1\u8d9f\u3002\u8fd9\u91cc\u8fd8\u7279\u522b\u7684\u589e\u52a0\u4e86\u4e00\u4e2a\u6807\u5fd7\u4f4dchange\uff0c\u53ea\u8981\u505a\u4e86\u9006\u5e8f\u4ea4\u6362change\u5c31\u4e3a\u771f\uff0c\u5982\u679c\u67d0\u4e00\u8d9f\u4e2d\u6ca1\u6709\u505a\u4efb\u4f55\u4ea4\u6362\uff0c\u8bf4\u660e\u5e8f\u5217\u5df2\u7ecf\u6709\u5e8f\uff0c\u5219change\u4e3a\u5047\uff0c\u6b64\u65f6\u6574\u4e2a\u6392\u5e8f\u53ef\u4ee5\u63d0\u524d\u505c\u6b62\u3002<\/p>\n<h3>2.\u5feb\u901f\u6392\u5e8f<\/h3>\n<p>\u5192\u6ce1\u7b97\u6cd5\u4e2d\u5bf9\u76f8\u90bb\u5143\u7d20\u7684\u4ea4\u6362\u4e00\u6b21\u53ea\u80fd\u6d88\u9664\u4e00\u4e2a\u9006\u5e8f\uff0c\u5982\u679c\u901a\u8fc7\u4ea4\u6362\u4e24\u4e2a\u4e0d\u76f8\u90bb\u7684\u5143\u7d20\u80fd\u4e00\u6b21\u6d88\u9664\u591a\u4e2a\u9006\u5e8f\uff0c\u5219\u4f1a\u589e\u52a0\u6574\u4e2a\u6392\u5e8f\u7684\u901f\u5ea6\u3002\u5feb\u901f\u6392\u5e8f\u5c31\u662f\u57fa\u4e8e\u8fd9\u6837\u7684\u76ee\u7684\uff0c\u5728\u4e00\u6b21\u4ea4\u6362\u4e2d\u53ef\u4ee5\u6d88\u9664\u591a\u4e2a\u9006\u5e8f\u3002<\/p>\n<p>\u5feb\u901f\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5728\u5f85\u6392\u5e8f\u8bb0\u5f55\u4e2d\u9009\u62e9\u4e00\u4e2a\u8bb0\u5f55\u4f5c\u4e3a\u6807\u51c6\uff0c\u5c06\u5927\u4e8e\u8be5\u6807\u51c6\u7684\u6240\u6709\u5143\u7d20\u79fb\u5230\u8be5\u6807\u51c6\u4e4b\u540e\uff0c\u5c06\u5c0f\u4e8e\u8be5\u6807\u51c6\u7684\u6240\u6709\u5143\u7d20\u79fb\u5230\u8be5\u6807\u51c6\u4e4b\u524d\u3002\u6700\u7ec8\u5c06\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u5206\u6210\u4e24\u4e2a\u5b50\u5e8f\u5217\uff0c\u800c\u6807\u51c6\u5143\u7d20\u63d2\u5165\u5230\u4e24\u4e2a\u5b50\u5e8f\u5217\u4e4b\u95f4\uff0c\u4e0a\u8ff0\u8fc7\u7a0b\u4e3a\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\u8fc7\u7a0b\u3002\u63a5\u4e0b\u6765\u518d\u5bf9\u4e24\u4e2a\u5b50\u5e8f\u5217\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u4e0d\u65ad\u7684\u5206\u5272\u5b50\u8868\uff0c\u76f4\u5230\u5b50\u8868\u957f\u5ea6\u4e0d\u8d85\u8fc71\u4e3a\u6b62\uff0c\u6b64\u65f6\u6240\u6709\u7684\u8bb0\u5f55\u90fd\u662f\u6709\u5e8f\u7684\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u8ff0\u5206\u6790\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u6765\u5b9e\u73b0\u5feb\u901f\u6392\u5e8f\u6cd5\u3002quickPass()\u7528\u4e8e\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\uff0c\u5e76\u4e14\u8fd4\u56de\u6807\u51c6\u5143\u7d20\u7684\u4f4d\u7f6epos\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid quickSort(int *r, int low, int high, int length)\r\n{\r\n\tint pos;\r\n\r\n\r\n\tif (low &lt; high) {\r\n\t\tpos = quickPass(r, low, high);\r\n\t\tquickSort(r, low, pos - 1, length);\r\n\t\tquickSort(r, pos + 1, high, length);\r\n\t\toutput(r, length);\r\n\t}\r\n}\r\n<\/pre>\n<p>quickPass()\u7528\u4e8e\u5b9e\u73b0\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\uff0c\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\u7ed3\u675f\u7684\u6761\u4ef6\u662flow\u4e0d\u5c0f\u4e8ehigh\u3002\u53ef\u4ee5\u770b\u5230\u5feb\u901f\u6392\u5e8f\u4e2d\u662f\u5c06low\u548chigh\u4e24\u4e2a\u4e0d\u76f8\u90bb\u4f4d\u7f6e\u7684\u5143\u7d20\u8fdb\u884c\u4ea4\u6362\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint quickPass(int *r, int left, int right) \r\n{\r\n\tint low, high;\r\n\r\n\tlow = left;\r\n\thigh = right;\r\n\tr&#x5B;0] = r&#x5B;low];\r\n\r\n\twhile (low &lt; high) {\r\n\t\twhile (low &lt; high &amp;&amp; r&#x5B;high] &gt;= r&#x5B;0])\r\n\t\t\thigh--;\r\n\t\tif (low &lt; high) {\r\n\t\t\tr&#x5B;low] = r&#x5B;high];\r\n\t\t\tlow++;\r\n\t\t}\r\n\r\n\t\twhile (low &lt; high &amp;&amp; r&#x5B;low] &lt;= r&#x5B;0])\r\n\t\t\tlow++;\r\n\t\tif (low &lt; high) {\r\n\t\t\tr&#x5B;high] = r&#x5B;low];\r\n\t\t\thigh--;\r\n\t\t}\r\n\t}\r\n\r\n\tr&#x5B;low] = r&#x5B;0];\r\n\treturn low;\r\n}\r\n<\/pre>\n<p>\u8fd9\u91cc\u4e5f\u7528\u5230\u4e86r[0]\uff0c\u7528\u5b83\u6765\u5b58\u50a8\u6807\u51c6\u5143\u7d20\uff0c\u5f53\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u5c06\u6807\u51c6\u5143\u7d20\u63d2\u5165low\u6240\u6307\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ea4\u6362\u7c7b\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u4ea4\u6362\u9006\u5e8f\u5143\u7d20\u800c\u6700\u7ec8\u8fbe\u5230\u6240\u6709\u5143\u7d20\u6709\u5e8f\uff0c\u8fd9\u91cc\u7684\u9006\u5e8f\u662f\u4e2a\u5e7f\u4e49\u6982\u5ff5\uff0c\u5982\u679c\u6309\u7167\u964d\u5e8f\u6392\u5e8f\uff0c\u90a3\u4e48\u524d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[215],"tags":[320,322,218,219],"class_list":["post-3812","post","type-post","status-publish","format-standard","hentry","category-215","tag-320","tag-322","tag-218","tag-219"],"views":4830,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3812","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=3812"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3812\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3812"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}