{"id":3797,"date":"2012-03-15T21:27:36","date_gmt":"2012-03-15T13:27:36","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3797"},"modified":"2012-03-30T21:55:37","modified_gmt":"2012-03-30T13:55:37","slug":"3797","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3797","title":{"rendered":"\u63d2\u5165\u7c7b\u6392\u5e8f"},"content":{"rendered":"<p>\u63d2\u5165\u7c7b\u6392\u5e8f\u7684\u57fa\u672c\u601d\u8def\u662f\u5728\u4e00\u4e2a\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u5b50\u8bb0\u5f55\u4e0a\uff0c\u6bcf\u4e00\u6b65\u5c06\u4e0b\u4e00\u4e2a\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u63d2\u5165\u5230\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u8bb0\u5f55\u5b50\u96c6\u4e2d\uff0c\u76f4\u5230\u5c06\u6240\u6709\u5f85\u6392\u5e8f\u8bb0\u5f55\u5168\u90e8\u63d2\u5165\u4e3a\u6b62\u3002<\/p>\n<h3>1.\u76f4\u63a5\u63d2\u5165\u6392\u5e8f<\/h3>\n<p>\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u662f\u6700\u57fa\u672c\u7684\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u4e00\u8d9f\u64cd\u4f5c\u662f\u5c06\u7b2ci\u4e2a\u8bb0\u5f55\u63d2\u5165\u5230\u524d\u9762i-1\u4e2a\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u8bb0\u5f55\u4e2d\uff0c\u5728\u67e5\u627e\u8bb0\u5f55i\u7684\u63d2\u5165\u4f4d\u7f6e\u65f6\uff0c\u4e5f\u5728\u8fdb\u884c\u5143\u7d20\u7684\u79fb\u52a8\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5f85\u6392\u5e8f\u961f\u5217r[1,length]\uff0c\u5219\u6574\u4e2a\u6392\u5e8f\u8fc7\u7a0b\u9700\u8981n-1\u6b21\u8d9f\u3002\u76f4\u63a5\u63d2\u5165\u7b97\u6cd5\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid insSort(int *r, int length)\r\n{\r\n\tint i, j;\r\n\r\n\tprintf(&quot;Sorting:\\n&quot;);\r\n\tfor ( i = 2; i &lt;= length; i++) {\r\n\t\tr&#x5B;0] = r&#x5B;i];\r\n\t\tj = i - 1;\r\n\r\n\t\twhile (r&#x5B;0] &lt; r&#x5B;j]) {\r\n\t\t\tr&#x5B;j + 1] = r&#x5B;j];\r\n\t\t\tj--;\r\n\t\t}\r\n\r\n\t\tr&#x5B;j + 1] = r&#x5B;0];\r\n\r\n\t\toutput(r, length);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u5177\u4f53\u5b9e\u73b0\u65f6\uff0c\u7528\u4e00\u7ef4\u6570\u7ec4\u6765\u5b58\u50a8\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\uff0c\u5176\u4e2d0\u53f7\u5143\u7d20\u5907\u4efd\u5f85\u63d2\u5165\u7684\u8bb0\u5f55\u3002<\/p>\n<h3>2.\u6298\u534a\u63d2\u5165\u6392\u5e8f<\/h3>\n<p>\u6298\u534a\u63d2\u5165\u6392\u5e8f\u6cd5\u4e0e\u76f4\u63a5\u63d2\u5165\u6cd5\u7c7b\u4f3c\uff0c\u533a\u522b\u5728\u4e8e\u786e\u5b9a\u5143\u7d20i\u63d2\u5165\u7684\u4f4d\u7f6e\u65f6\u5229\u7528\u6298\u534a\u67e5\u627e\u6cd5\u3002\u6bcf\u4e00\u8d9f\u6392\u5e8f\u7684\u8fc7\u7a0b\u662f\u5148\u7528\u6298\u534a\u67e5\u627e\u6cd5\u786e\u5b9a\u63d2\u5165\u4f4d\u7f6e\uff0c\u518d\u9010\u4e2a\u8fdb\u884c\u5143\u7d20\u7684\u79fb\u52a8\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid binSort(int *r, int length)\r\n{\r\n\tint i, j;\r\n\tint low ,high, mid;\r\n\r\n\tprintf(&quot;Sorting:\\n&quot;);\r\n\tfor ( i = 2; i &lt;= length; i++) {\r\n\t\tr&#x5B;0] = r&#x5B;i];\r\n\t\tlow = 1;\r\n\t\thigh = i - 1;\r\n\r\n\t\twhile (low &lt;= high) {\r\n\t\t\tmid = (low + high) \/ 2;\r\n\t\t\tif (r&#x5B;0] &lt; r&#x5B;mid])\r\n\t\t\t\thigh = mid - 1;\r\n\t\t\telse\r\n\t\t\t\tlow = mid + 1;\r\n\t\t}\r\n\r\n\t\tfor (j = i - 1; j &gt;= low; j--) \r\n\t\t\tr&#x5B;j + 1] = r&#x5B;j];\r\n\t\tr&#x5B;high + 1] = r&#x5B;0];\r\n\t\toutput(r, length);\r\n\r\n\t}\r\n}\r\n<\/pre>\n<p>\u4e0e\u76f4\u63a5\u63d2\u5165\u6cd5\u7c7b\u4f3c\uff0c\u6570\u7ec4r\u4e2d\u76840\u53f7\u5143\u7d20\u4e5f\u5907\u4efd\u4e86\u5f85\u63d2\u5165\u7684\u5143\u7d20i\u3002\u5f53\u786e\u5b9a\u4e86\u8bb0\u5f55i\u7684\u4f4d\u7f6e\u65f6\uff0c\u6b64\u65f6\u5b58\u5728low=high+1\u8fd9\u6837\u7684\u5173\u7cfb\uff0c\u63a5\u4e0b\u6765\u5c06low\u5230i-1\u4e4b\u95f4\u7684\u5143\u7d20\u90fd\u540e\u79fb\u4e00\u4f4d\uff0c\u6700\u7ec8\u8bb0\u5f55i\u521a\u597d\u63d2\u5165\u7a7a\u51fa\u6765\u7684\u4f4d\u7f6e\u4e2d\u3002<\/p>\n<h3>3.\u5e0c\u5c14\u6392\u5e8f<\/h3>\n<p>\u6574\u4e2a\u5e0c\u5c14\u6392\u5e8f\u7684\u8fc7\u7a0b\u7531\u82e5\u5e72\u6b21\u5e0c\u5c14\u63d2\u5165\u7ec4\u6210\uff0c\u5177\u4f53\u6b21\u6570\u7531\u589e\u91cf\u6570\u7ec4delta\u4e2d\u7684\u5143\u7d20\u4e2a\u6570n\u786e\u5b9a\u3002\u5728\u6bcf\u4e00\u6b21\u7684\u5e0c\u5c14\u63d2\u5165\u7b97\u6cd5\u4e2d\uff0c\u5c06\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u5e8f\u5217\u5206\u6210d\u4e2a\u7a00\u758f\u5b50\u5e8f\u5217\uff0c\u6bcf\u4e2a\u7a00\u758f\u5b50\u5e8f\u5217\u4e2d\u5143\u7d20\u4e4b\u95f4\u7684\u95f4\u9694\u6b63\u597d\u4e3ad\u3002\u5e0c\u5c14\u63d2\u5165\u7b97\u6cd5\u5c31\u662f\u5c06\u6bcf\u4e00\u4e2a\u5b50\u5e8f\u5217\u90fd\u6309\u7167\u76f4\u63a5\u63d2\u5165\u6cd5\u6392\u5217\u6210\u6709\u5e8f\uff0c\u4ece\u800c\u4f7f\u5f97\u6574\u4e2a\u5e8f\u5217\u57fa\u672c\u6709\u5e8f\u3002\u4e0a\u8ff0\u8fc7\u7a0b\u4f1a\u91cd\u590dn\u6b21\uff0c\u5c31\u662f\u5e0c\u5c14\u6392\u5e8f\u7b97\u6cd5\u7684\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid shellSort(int *r, int length, int *delta, int n)\r\n{\r\n\tint i;\r\n\tfor ( i = 0; i &lt; n; i++) {\r\n\t\tshellInsert(r, length, delta&#x5B;i]);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u7b2ci\u8d9f\u5e0c\u5c14\u6392\u5e8f\u4e2d\uff0c\u6bcf\u4e2a\u7a00\u758f\u5b50\u5e8f\u5217\u4e2d\u5143\u7d20\u7684\u95f4\u9694\u7531delta[i]\u51b3\u5b9a\u3002\u4f46\u5e0c\u5c14\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u8d9f\u6392\u5e8f\uff0c\u5143\u7d20\u7684\u95f4\u9694\u5fc5\u9700\u662f1\u3002\u56e0\u4e3a\u6700\u540e\u4e00\u6b21\u5e0c\u5c14\u6392\u5e8f\u5c31\u76f8\u5f53\u4e8e\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\uff0c\u4f46\u662f\u6b64\u65f6\u6574\u4e2a\u8bb0\u5f55\u5e8f\u5217\u5df2\u7ecf\u51e0\u4e4e\u6709\u5e8f\uff0c\u56e0\u6b64\u79fb\u52a8\u7684\u6b21\u6570\u4f1a\u5927\u5927\u51cf\u5c11\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid shellInsert(int *r, int length, int d)\r\n{\r\n\tint i, j;\r\n\tint k;\r\n\t\r\n\tfor (i = 1 + d; i &lt;= length; i++) {\r\n\t\tif (r&#x5B;i] &lt; r&#x5B;i - d]) {\r\n\t\t\tr&#x5B;0] = r&#x5B;i];\r\n\t\t\t\r\n\t\t\tfor (j = i - d; j &gt; 0 &amp;&amp; r&#x5B;0] &lt; r&#x5B;j]; j -= d) {\r\n\t\t\t\tr&#x5B;j + d] = r&#x5B;j];\r\n\t\t\t}\r\n\t\t\tr&#x5B;j + d] = r&#x5B;0];\r\n\t\t}\r\n\t}\r\n\toutput(r, length);\r\n}<\/pre>\n<p>\u867d\u7136\u5e0c\u5c14\u63d2\u5165\u7b97\u6cd5\u4e2d\u9700\u8981\u4f9d\u6b21\u5c06d\u4e2a\u5b50\u5e8f\u5217\u6392\u6210\u6709\u5e8f\uff0c\u4f46\u662f\u5b9e\u9645\u7684\u5b9e\u73b0\u8fc7\u7a0b\u5374\u4ece\u7b2c\u4e00\u4e2a\u5b50\u5e8f\u5217\u7684\u7b2c\u4e8c\u4e2a\u8bb0\u5f55(d+1)\u5f00\u59cb\u4f9d\u6b21\u904d\u5386\u6574\u4e2a\u5e8f\u5217\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u7531\u95f4\u9694d\u63a7\u5236\u7684\uff0c\u56e0\u6b64\u5c31\u76f8\u5f53\u4e8e\u6bcf\u4e2a\u5b50\u5e8f\u5217\u5404\u81ea\u6392\u5e8f\u3002\u5185\u90e8\u7684for\u5faa\u73af\u76f8\u5f53\u4e8e\u5bf9\u6bcf\u4e2a\u5b50\u5e8f\u5217\u8fdb\u884c\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\uff0c\u4ece\u5f53\u524d\u7684\u8bb0\u5f55i(\u4fdd\u5b58\u5728r[0]\u4e2d)\u5f00\u59cb\uff0c\u4f9d\u6b21\u626b\u63cf\u5f53\u524d\u5b50\u5e8f\u5217\u4e4b\u524d\u7684\u5143\u7d20(\u6bcf\u4e2a\u5143\u7d20\u7684\u95f4\u9694\u4e3ad\uff0c\u56e0\u6b64\u6bcf\u6b21\u5faa\u73afj\u90fd\u51cf\u5c11d)\u4ee5\u786e\u4fdd\u63d2\u5165\u4f55\u65f6\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u672c\u6587\u6240\u6d89\u53ca\u7684\u6392\u5e8f\u7b97\u6cd5\u6e90\u7801\u53ef\u4ee5\u5728<a href=\"http:\/\/edsionte.download.csdn.net\/\" target=\"_blank\">\u8fd9\u91cc<\/a>\u4e0b\u8f7d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d2\u5165\u7c7b\u6392\u5e8f\u7684\u57fa\u672c\u601d\u8def\u662f\u5728\u4e00\u4e2a\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u5b50\u8bb0\u5f55\u4e0a\uff0c\u6bcf\u4e00\u6b65\u5c06\u4e0b\u4e00\u4e2a\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u63d2\u5165\u5230\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u8bb0\u5f55\u5b50\u96c6\u4e2d\uff0c\u76f4\u5230 [&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":[319,318,219,317],"class_list":["post-3797","post","type-post","status-publish","format-standard","hentry","category-215","tag-319","tag-318","tag-219","tag-317"],"views":4338,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3797","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=3797"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3797\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3797"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}