{"id":2898,"date":"2011-04-13T15:22:03","date_gmt":"2011-04-13T07:22:03","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2898"},"modified":"2011-04-14T16:27:33","modified_gmt":"2011-04-14T08:27:33","slug":"%e5%88%86%e6%b2%bb%e7%ae%97%e6%b3%95%e4%b9%8b%e5%90%88%e5%b9%b6%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2898","title":{"rendered":"\u5206\u6cbb\u7b97\u6cd5\u4e4b\u5408\u5e76\u6392\u5e8f"},"content":{"rendered":"<p>\u5206\u6cbb\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u4e00\u4e2a\u89c4\u6a21\u4e3an\u7684\u95ee\u9898\u5206\u89e3\u6210k\u4e2a\u89c4\u6a21\u8f83\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u76f8\u4e92\u72ec\u7acb\u5e76\u4e14\u4e0e\u539f\u95ee\u9898\u76f8\u540c\u3002\u5148\u9012\u5f52\u7684\u89e3\u51b3\u8fd9\u4e9b\u5b50\u95ee\u9898\uff0c\u7136\u540e\u518d\u5c06\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u5230\u539f\u95ee\u9898\u7684\u89e3\u5f53\u4e2d\u3002<\/p>\n<p>\u5408\u5e76\u6392\u5e8f\u7b97\u6cd5\u662f\u7528\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u5bf9n\u4e2a\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u7684\u7b97\u6cd5\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u5c06\u5f85\u6392\u5e8f\u5143\u7d20\u5206\u6210\u5927\u5c0f\u5927\u81f4\u76f8\u540c\u7684\u4e24\u4e2a\u5b50\u96c6\u5408\uff0c\u5206\u522b\u5bf9\u4e24\u4e2a\u5b50\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u7ec8\u5c06\u6392\u597d\u5e8f\u7684\u4e24\u4e2a\u5b50\u96c6\u5408\u5408\u5e76\u6210\u4e00\u4e2a\u6392\u597d\u5e8f\u7684\u96c6\u5408\u3002\u5408\u5e76\u6392\u5e8f\u7b97\u6cd5\u53ef\u9012\u5f52\u7684\u4f2a\u4ee3\u7801\u8868\u8fbe\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">void mergeSort(int *a, int left, int right)\r\n{\r\n\tint i;\r\n\tif (left &lt; right) {\r\n\t\ti = (left + right) \/ 2;\r\n\t\tmergeSort(a, left, i);\r\n\t\tmergeSort(a, i + 1, right);\r\n\t\tmerge(a, b, left, i, right);\r\n\t\tcopy(a, b, left, right);\r\n\t}\r\n}<\/pre>\n<p>\u5728mergeSort\u51fd\u6570\u5185\uff0c\u4e0d\u65ad\u7684\u8fdb\u884c\u9012\u5f52\u8c03\u7528\u4ee5\u7f29\u5c0f\u95ee\u9898\u89c4\u6a21\uff1bmerge\u51fd\u6570\u7528\u4e8e\u5c06\u4e24\u4e2a\u6392\u597d\u5e8f\u7684\u5b50\u5e8f\u5217\u5408\u5e76\u6210\u4e00\u4e2a\u5927\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u5e76\u5b58\u50a8\u5728\u6570\u7ec4b\u4e2d\uff1b\u6700\u540e\u5229\u7528copy\u51fd\u6570\u5c06b\u6570\u7ec4\u7684\u5e8f\u5217\u518d\u91cd\u65b0\u62f7\u8d1d\u5230\u6570\u7ec4a\u4e2d\uff0c\u5b8c\u6210\u5408\u5e76\u6392\u5e8f\u3002<\/p>\n<p>\u4e0a\u8ff0\u7684mergeSort\u51fd\u6570\u4e2d\uff0c\u9012\u5f52\u8c03\u7528\u662f\u6574\u4e2a\u7b97\u6cd5\u7684\u5173\u952e\u6b65\u9aa4\u3002mergeSort\u51fd\u6570\u4e0d\u65ad\u7684\u5e73\u5206\u5f85\u6392\u5e8f\u7684\u6570\u5217\u96c6\u5408\uff0c\u5982\u679c\u5f53\u524d\u6570\u5217\u96c6\u5408\u4e24\u7aef\u7684\u5e8f\u53f7\u5206\u522b\u4e3aleft\u548cright\uff0c\u90a3\u4e48\u5e73\u5206\u540e\u7684\u4e24\u4e2a\u6570\u5217\u96c6\u5408\u5e8f\u53f7\u5206\u522b\u662fleft\u5230i\u548ci+1\u5230right\u3002\u8fd9\u79cd\u4e0d\u65ad\u5e73\u5206\u5e76\u9012\u5f52\u7684\u7ed3\u679c\u662f\u4f7f\u5f97\u5f53\u524d\u7684\u5f85\u6392\u5e8f\u96c6\u5408\u4e2d\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u63a5\u7740\u518d\u8fdb\u884c\u4e24\u4e24\u5b50\u5e8f\u5217\u96c6\u5408\u7684\u5408\u5e76\u3002<\/p>\n<p>\u6b63\u5982\u4e0a\u9762\u6240\u8bf4\u7684\uff0c\u8fd9\u4e9b\u5f85\u5408\u5e76\u7684\u5b50\u5e8f\u5217\u90fd\u5df2\u6392\u597d\u5e8f\u3002\u5e76\u4e14\u6700\u521d\u4e00\u6279\u5f85\u5408\u5e76\u7684\u5b50\u5e8f\u5217\u96c6\u5408\u4e2d\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5408\u5e76\u540e\u5143\u7d20\u6570\u91cf\u4e3a2\uff0c\u518d\u6b21\u5408\u5e76\u4e3a4\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u8ff0\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u9012\u5f52\u5f62\u5f0f\u7684\u5408\u5e76\u6392\u5e8f\u7b97\u6cd5\u6539\u8fdb\u6210\u975e\u9012\u5f52\u7684\u5f62\u5f0f\u3002\u5728\u4e0a\u8ff0\u9012\u5f52\u5f62\u5f0f\u4e2d\uff0c\u6211\u4eec\u662f\u4ece\u6574\u4e2a\u5e8f\u5217\u51fa\u53d1\uff0c\u9010\u6e10\u5e73\u5206\u518d\u9012\u5f52\u3002\u800c\u975e\u9012\u5f52\u5f62\u5f0f\u7684\u6392\u5e8f\u7b97\u6cd5\u5219\u5148\u8ba9\u6574\u4e2a\u5e8f\u5217\u4e2d\u76f8\u90bb\u7684\u5143\u7d20\u4e24\u4e24\u8fdb\u884c\u6392\u5e8f\uff0c\u5f62\u6210n\/2\u4e2a\u957f\u5ea6\u4e3a2\u7684\u5df2\u6392\u597d\u5e8f\u7684\u5b50\u5e8f\u5217\u3002\u63a5\u7740\u518d\u5c06\u5b83\u4eec\u6392\u6210\u957f\u5ea6\u4e3a4\u7684\u5b50\u5e8f\u5217\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u8be5\u7b97\u6cd5\u7ed3\u675f\u7ed3\u675f\u65f6\u662f\u5c06\u4e24\u4e2a\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u5b50\u5e8f\u5217\u6392\u6210\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u601d\u8def\uff0c\u975e\u9012\u5f52\u5f62\u5f0f\u7684\u5408\u5e76\u7b97\u6cd5\u53ef\u53c2\u8003\u5982\u4e0b\u4ee3\u7801\u3002mergeSort\u51fd\u6570\u4f9d\u6b21\u5bf9\u6574\u4e2a\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\u4e2d\u957f\u5ea6\u4e3a1\u30012\u30014\u30018\u7684\u5b50\u5e8f\u5217\u8fdb\u884c\u6392\u5e8f\u3002s\u5373\u4e3a\u5f53\u524d\u6b63\u8fdb\u884c\u6392\u5e8f\u7684\u5b50\u5e8f\u5217\u96c6\u5408\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<pre class=\"brush:c\">void mergeSort(int a[], int n)\r\n{\r\n\tint *b = NULL;\r\n\tint s = 1;\r\n\tint count = 1;\r\n\r\n\tb = (int *)malloc(sizeof(int) * n);\r\n\twhile (s &lt; n) {\r\n\t\tprintf(\"sort %d:\\n\", count++);\r\n\t\tmergePass(a, b, s, n);\r\n\t\ts += s;\r\n\t\tprintf(\"sort %d:\\n\", count++);\r\n\t\tmergePass(b, a, s, n);\r\n\t\ts += s;\r\n\t}\r\n\tfree(b);\r\n}<\/pre>\n<p>mergePass\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5927\u5c0f\u4e3as\u7684\u76f8\u90bb\u5b50\u5e8f\u5217\u3002\u901a\u8fc7while\u5faa\u73af\u5c06\u6574\u4e2a\u5e8f\u5217\u5206\u6210n\/s\u4e2a\u5927\u5c0f\u4e3as\u7684\u5b50\u5e8f\u5217\uff0c\u7531\u4e8e\u8fd9\u5199\u5b50\u5e8f\u5217\u5185\u90e8\u5df2\u7ecf\u6392\u597d\u5e8f\uff0c\u5219\u8c03\u7528merge\u51fd\u6570\u76f4\u63a5\u8fdb\u884c\u5408\u5e76\u5373\u53ef\u3002<\/p>\n<pre class=\"brush:c\">void mergePass(int x[], int y[], int s, int n)\r\n{\r\n\tint i = 0;\r\n\tint j;\r\n\r\n\twhile (i &lt; n - 2 * s) {\r\n\t\tmerge(x, y, i, i + s - 1, i + 2 * s -1);\r\n\t\ti = i + 2 * s;\r\n\t}\r\n\r\n\tif (i + s &lt; n)\r\n\t\tmerge(x, y, i, i + s - 1, n - 1);\r\n\telse\r\n\t\tfor (j = i; j &lt;= n - 1; j++)\r\n\t\t\ty[j] = x[j];\r\n\r\n\tfor (i = 0; i &lt; n; i++)\r\n\t\tprintf(\"%d \", y[i]);\r\n\tprintf(\"\\n\");\r\n}<\/pre>\n<p>merge\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5408\u5e76\u4e24\u4e2a\u76f8\u90bb\u7684\u5b50\u5e8f\u5217\uff0c\u8fd9\u4e24\u4e2a\u5b50\u5e8f\u5217\u7684\u5e8f\u53f7\u5206\u522b\u4e3al\u5230m\u548cm+1\u5230r\u3002<\/p>\n<pre class=\"brush:c\">\r\nvoid merge(int c[], int d[], int l, int m, int r) \r\n{\r\n\tint i, j, k;\r\n\t\r\n\ti = l;\r\n\tj = m + 1;\r\n\tk = l;\r\n\twhile ((i <= m) &#038;&#038; (j <= r))\r\n\t\tif (c[i] <= c[j])\r\n\t\t\td[k++] = c[i++];\r\n\t\telse \r\n\t\t\td[k++] = c[j++];\r\n\t\r\n\tint q;\r\n\tif (i > m)\r\n\t\tfor (q = j; q <= r; q++)\r\n\t\t\td[k++] = c[q];\r\n\telse\r\n\t\tfor (q = i; q <= m; q++)\r\n\t\t\td[k++] = c[q];\r\n}\r\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5408\u5e76\u7b97\u6cd5\u7684\u6838\u5fc3\u51fd\u6570\uff0c\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u4ece\u8fd9\u91cc<a href=\"http:\/\/edsionte.download.csdn.net\/\" target=\"_blank\">\u4e0b\u8f7d<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u6cbb\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u4e00\u4e2a\u89c4\u6a21\u4e3an\u7684\u95ee\u9898\u5206\u89e3\u6210k\u4e2a\u89c4\u6a21\u8f83\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u76f8\u4e92\u72ec\u7acb\u5e76\u4e14\u4e0e\u539f\u95ee\u9898\u76f8\u540c\u3002\u5148\u9012\u5f52 [&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,217,406],"class_list":["post-2898","post","type-post","status-publish","format-standard","hentry","category-215","tag-c","tag-216","tag-217","tag-406"],"views":7809,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2898","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=2898"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2898\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2898"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}