{"id":222,"date":"2025-07-29T19:54:52","date_gmt":"2025-07-29T11:54:52","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=222"},"modified":"2025-11-24T16:39:55","modified_gmt":"2025-11-24T08:39:55","slug":"%e5%bd%92%e5%b9%b6%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=222","title":{"rendered":"\u5f52\u5e76\u6392\u5e8f"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"811\" height=\"505\" data-id=\"223\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/\u5f52\u5e76\u6392\u5e8f.gif\" alt=\"\" class=\"wp-image-223\"\/><\/figure>\n<\/figure>\n\n\n\n<p>\u5f52\u5e76\u6392\u5e8f\uff1a\u5f52\u5e76\u6392\u5e8f\uff08MERGE-SORT\uff09\u662f\u5efa\u7acb\u5728\u5f52\u5e76\u64cd\u4f5c\u4e0a\u7684\u4e00\u79cd\u6709\u6548\u7684\u6392\u5e8f\u7b97\u6cd5,\u8be5\u7b97\u6cd5\u662f\u91c7\u7528\u5206\u6cbb\u6cd5\uff08Divide and Conquer\uff09\u7684\u4e00\u4e2a\u975e\u5e38\u5178\u578b\u7684\u5e94\u7528\u3002\u5c06\u5df2\u6709\u5e8f\u7684\u5b50\u5e8f\u5217\u5408\u5e76\uff0c\u5f97\u5230\u5b8c\u5168\u6709\u5e8f\u7684\u5e8f\u5217\uff1b\u5373\u5148\u4f7f\u6bcf\u4e2a\u5b50\u5e8f\u5217\u6709\u5e8f\uff0c\u518d\u4f7f\u5b50\u5e8f\u5217\u6bb5\u95f4\u6709\u5e8f\u3002\u82e5\u5c06\u4e24\u4e2a\u6709\u5e8f\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u6709\u5e8f\u8868\uff0c\u79f0\u4e3a\u4e8c\u8def\u5f52\u5e76\u3002 \u5f52\u5e76\u6392\u5e8f\u6838\u5fc3\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<p><strong class=\"\">1. \u6838\u5fc3\u601d\u60f3<\/strong>\uff1a<\/p>\n\n\n\n<p><strong>\u6cbb<\/strong>\uff1a\u5c06\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u5173\u952e\u6b65\u9aa4\uff09\u3002<\/p>\n\n\n\n<p><strong>\u5206<\/strong>\uff1a\u5c06\u6570\u7ec4\u9012\u5f52\u62c6\u5206\u6210\u4e24\u534a\uff0c\u76f4\u5230\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a1\uff08\u5929\u7136\u6709\u5e8f\uff09\u3002<\/p>\n\n\n\n<p>\u9012\u5f52\u6cd5\uff1a<\/p>\n\n\n\n<p>\u9012\u5f52\u6cd5\u8be6\u89e3\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\u662f\u5355\u8d9f\uff1a\u5f53\u7ed9\u4f60\u4e00\u4e2a\u533a\u95f4\u6570\u7ec4\u548c\u6240\u9700\u7a7a\u95f4\u548c\u7a7a\u95f4\u8d77\u59cb\u70b9\u548c\u7ed3\u675f\u70b9begin,end<\/p>\n\n\n\n<p>\u9996\u5148\u5224\u65ad\u662f\u5426\u4e3a\u4e00\uff1a\u4e3a\u4e00\u5c31\u7ed3\u675f\u51fd\u6570<\/p>\n\n\n\n<p>\u7136\u540e\u5c06\u5176\u5207\u5206\u6210\u5de6\u53f3\u4e24\u8d9f\u6392\u597d<\/p>\n\n\n\n<p>\u6700\u540e\u5f00\u59cb\u6392\u81ea\u5df1\u7684\u4e00\u8d9f\uff0c\u5f53begin2\u5927\u4e8ebegin1\u65f6\u5c31\u5199\u5165tmp\u6570\u7ec4\uff0c\u7ed3\u675f\u6761\u4ef6\uff1a\u4efb\u610f\u4e00\u4e2abegin\u5230end\u65f6\u7ed3\u675f<\/p>\n\n\n\n<p>\u7136\u540e\u518d\u904d\u5386\u4e24\u4e2abegin\uff0c\u6ca1\u6709\u904d\u5386\u5b8c\u7684\u90e8\u5206<\/p>\n\n\n\n<p>\u6700\u540e\u5c06tmp\u4e2d\u7684\u6570\u636ecopy\u5230\u6307\u5b9a\u6570\u7ec4\u4e2d\u53bb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void _MergeSort(vector<int>& a, vector<int>& tmp, int begin, int end) {\n\tif (begin >= end) return;\n\tint mid = (begin + end) \/ 2;\n\t_MergeSort(a, tmp, begin, mid);\n\t_MergeSort(a, tmp, mid + 1, end);\n\tint begin1 = begin, end1 = mid;\n\tint begin2 = mid + 1, end2 = end;\n\tint i = begin;\n\twhile (begin1 <= end1 &#038;&#038; begin2 <= end2) {\n\t\tif (a[begin1] <= a[begin2])\n\t\t\ttmp[i++] = a[begin1++];\n\t\telse\n\t\t\ttmp[i++] = a[begin2++];\n\t}\n\twhile (begin1 <= end1) tmp[i++] = a[begin1++];\n\twhile (begin2 <= end2) tmp[i++] = a[begin2++];\n\tcopy(tmp.begin() + begin, tmp.begin() + end + 1, a.begin() + begin);\n}\nvoid MergeSort(vector<int>& a) {\n\tif (a.empty()) return;\n\tvector<int> tmp(a.size());\n\t_MergeSort(a, tmp, 0, static_cast<int>(a.size() - 1));\n}<\/code><\/pre>\n\n\n\n<p>\u975e\u9012\u5f52\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void xunMerge(vector<int>& a) {\n\tif (a.empty()) return;\n\tvector<int> tmp(a.size());\n\tint gap = 1;\n\tint n = a.size();\n\twhile (gap < n) {\n\t\tfor (int i = 0; i < n; i += 2 * gap) {\n\t\t\tint begin1 = i, end1 = min(i + gap - 1, n - 1);\n\t\t\tint begin2 = i + gap, end2 = min(i + 2 * gap - 1, n - 1);\n\t\t\tif (begin2 >= n) { \/\/ \u7b2c\u4e8c\u7ec4\u5168\u90e8\u8d8a\u754c\uff0c\u65e0\u9700\u5f52\u5e76\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tint j = i;\n\t\t\twhile (begin1 <= end1 &#038;&#038; begin2 <= end2) {\n\t\t\t\tif (a[begin1] <= a[begin2])\n\t\t\t\t\ttmp[j++] = a[begin1++];\n\t\t\t\telse\n\t\t\t\t\ttmp[j++] = a[begin2++];\n\t\t\t}\n\t\t\twhile (begin1 <= end1) tmp[j++] = a[begin1++];\n\t\t\twhile (begin2 <= end2) tmp[j++] = a[begin2++];\n\t\t\t\/\/ \u590d\u5236\u5df2\u5f52\u5e76\u7684\u90e8\u5206\n\t\t\tcopy(tmp.begin() + i, tmp.begin() + j, a.begin() + i);\n\t\t}\n\t\tgap *= 2;\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u4e86\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f52\u5e76\u6392\u5e8f\uff1a\u5f52\u5e76\u6392\u5e8f\uff08MERGE-SORT\uff09\u662f\u5efa\u7acb\u5728\u5f52\u5e76\u64cd\u4f5c\u4e0a\u7684\u4e00\u79cd\u6709\u6548\u7684\u6392\u5e8f\u7b97\u6cd5,\u8be5\u7b97\u6cd5\u662f\u91c7\u7528\u5206\u6cbb\u6cd5\uff08Divi&#8230;<\/p>\n","protected":false},"author":1,"featured_media":223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-6"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=222"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":860,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions\/860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/223"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}