{"id":200,"date":"2025-07-23T20:06:19","date_gmt":"2025-07-23T12:06:19","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=200"},"modified":"2025-11-24T16:40:28","modified_gmt":"2025-11-24T08:40:28","slug":"%e6%8e%92%e5%ba%8f%ef%bc%881%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=200","title":{"rendered":"\u6392\u5e8f\uff081\uff09"},"content":{"rendered":"\n<p>1.\u6392\u5e8f\u7684\u6982\u5ff5\u53ca\u5176\u8fd0\u7528<\/p>\n\n\n\n<p>\u6392\u5e8f\u7684\u6982\u5ff5\u6392\u5e8f\uff1a\u6240\u8c13\u6392\u5e8f\uff0c\u5c31\u662f\u4f7f\u4e00\u4e32\u8bb0\u5f55\uff0c\u6309\u7167\u5176\u4e2d\u7684\u67d0\u4e2a\u6216\u67d0\u4e9b\u5173\u952e\u5b57\u7684\u5927\u5c0f\uff0c\u9012\u589e\u6216\u9012\u51cf\u7684\u6392\u5217\u8d77\u6765\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u7a33\u5b9a\u6027\uff1a\u5047\u5b9a\u5728\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u5e8f\u5217\u4e2d\uff0c\u5b58\u5728\u591a\u4e2a\u5177\u6709\u76f8\u540c\u7684\u5173\u952e\u5b57\u7684\u8bb0\u5f55\uff0c\u82e5\u7ecf\u8fc7\u6392\u5e8f\uff0c\u8fd9\u4e9b\u8bb0\u5f55\u7684\u76f8\u5bf9\u6b21\u5e8f\u4fdd\u6301\u4e0d\u53d8\uff0c\u5373\u5728\u539f\u5e8f\u5217\u4e2d\uff0cr[i]=r[j]\uff0c\u4e14r[i]\u5728r[j]\u4e4b\u524d\uff0c\u800c\u5728\u6392\u5e8f\u540e\u7684\u5e8f\u5217\u4e2d\uff0cr[i]\u4ecd\u5728r[j]\u4e4b\u524d\uff0c\u5219\u79f0\u8fd9\u79cd\u6392\u5e8f\u7b97\u6cd5\u662f\u7a33\u5b9a\u7684\uff1b\u5426\u5219\u79f0\u4e3a\u4e0d\u7a33\u5b9a\u7684\u3002<\/p>\n\n\n\n<p>\u5185\u90e8\u6392\u5e8f\uff1a\u6570\u636e\u5143\u7d20\u5168\u90e8\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u5916\u90e8\u6392\u5e8f\uff1a\u6570\u636e\u5143\u7d20\u592a\u591a\u4e0d\u80fd\u540c\u65f6\u653e\u5728\u5185\u5b58\u4e2d\uff0c\u6839\u636e\u6392\u5e8f\u8fc7\u7a0b\u7684\u8981\u6c42\u4e0d\u65ad\u5730\u5728\u5185\u5916\u5b58\u4e4b\u95f4\u79fb\u52a8\u6570\u636e\u7684\u6392\u5e8f<\/p>\n\n\n\n<p>\u4eca\u5929\u6211\u4e3b\u8981\u5411\u5927\u5bb6\u4ecb\u7ecd\u4e94\u79cd\u6392\u5e8f\u7b97\u6cd5\uff08\u5192\u6ce1\uff0c\u63d2\u5165\uff0c\u5e0c\u5c14\uff0c\u5806\uff0c\u9009\u62e9\uff09<\/p>\n\n\n\n<p>\u5192\u6ce1\u6392\u5e8f\uff1a<\/p>\n\n\n\n<p>\u57fa\u672c\u601d\u60f3\u5b83\u901a\u8fc7\u91cd\u590d\u5730\u904d\u5386\u5f85\u6392\u5e8f\u7684\u5217\u8868\uff0c\u6bd4\u8f83\u76f8\u90bb\u7684\u5143\u7d20\u5e76\u4ea4\u6362\u5b83\u4eec\u7684\u4f4d\u7f6e\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u8be5\u7b97\u6cd5\u7684\u540d\u79f0\u6765\u6e90\u4e8e\u8f83\u5c0f\u7684\u5143\u7d20\u4f1a\u50cf&#8221;\u6c14\u6ce1&#8221;\u4e00\u6837\u9010\u6e10&#8221;\u6d6e&#8221;\u5230\u5217\u8868\u7684\u9876\u7aef\u3002<\/p>\n\n\n\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=\"826\" height=\"257\" data-id=\"202\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/\u5192\u6ce1\u6392\u5e8f.gif\" alt=\"\" class=\"wp-image-202\"\/><\/figure>\n<\/figure>\n\n\n\n<p>\u5982\u4e0a\u56fe<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u5192\u6ce1\u6392\u5e8f\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5192\u6ce1\nvoid Bubblesort(vector<int>&t) {\n\tfor (int i = 0; i < t.size(); i++) {\n\t\tfor (int j = 0; j < t.size()-1-i; j++) {\n\t\t\tif (t[j] > t[j + 1]) {\n\t\t\t\tswap(t[j], t[j + 1]);\n\t\t\t}\n\t\t}\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>\u63d2\u5165\u6392\u5e8f\uff1a\u628a\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u6309\u5176\u5173\u952e\u7801\u503c\u7684\u5927\u5c0f\u9010\u4e2a\u63d2\u5165\u5230\u4e00\u4e2a\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u6709\u5e8f\u5e8f\u5217\u4e2d\uff0c\u76f4\u5230\u6240\u6709\u7684\u8bb0\u5f55\u63d2\u5165\u5b8c\u4e3a\u6b62\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6709\u5e8f\u5e8f\u5217 \u3002<br><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 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=\"203\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/\u63d2\u5165\u6392\u5e8f.gif\" alt=\"\" class=\"wp-image-203\"\/><\/figure>\n<\/figure>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u63d2\u5165\nvoid insertsort(vector<int>&t) {\n\tfor (int i = 0; i < t.size(); i++) {\n\t\tint end = i;\n\t\twhile (end > 0) {\n\t\t\tif (t[end] <= t[end - 1]) {\n\t\t\t\tswap(t[end], t[end - 1]);\n\t\t\t\tend--;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t\n\t\t}\n\t}\n\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u7684\u7279\u6027\u603b\u7ed3\uff1a\n1. \u5143\u7d20\u96c6\u5408\u8d8a\u63a5\u8fd1\u6709\u5e8f\uff0c\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u8d8a\u9ad8\n2. \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N^2)\n3. \u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)\uff0c\u5b83\u662f\u4e00\u79cd\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\n4. \u7a33\u5b9a\u6027\uff1a\u7a33\u5b9a<\/code><\/pre>\n\n\n\n<p>\u5e0c\u5c14\u6392\u5e8f\uff1a<\/p>\n\n\n\n<p>\u5e0c\u5c14\u6392\u5e8f\u6cd5\u53c8\u79f0\u7f29\u5c0f\u589e\u91cf\u6cd5\u3002\u5e0c\u5c14\u6392\u5e8f\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\uff1a\u5148\u9009\u5b9a\u4e00\u4e2a\u6574\u6570\uff0c\u628a\u5f85\u6392\u5e8f\u6587\u4ef6\u4e2d\u6240\u6709\u8bb0\u5f55\u5206\u6210\u4e2a\u7ec4\uff0c\u6240\u6709\u8ddd\u79bb\u4e3a\u7684\u8bb0\u5f55\u5206\u5728\u540c\u4e00\u7ec4\u5185\uff0c\u5e76\u5bf9\u6bcf\u4e00\u7ec4\u5185\u7684\u8bb0\u5f55\u8fdb\u884c\u6392\u5e8f\u3002\u7136\u540e\uff0c\u53d6\uff0c\u91cd\u590d\u4e0a\u8ff0\u5206\u7ec4\u548c\u6392\u5e8f\u7684\u5de5\u4f5c\u3002\u5f53\u5230\u8fbe=1\u65f6\uff0c\u6240\u6709\u8bb0\u5f55\u5728\u7edf\u4e00\u7ec4\u5185\u6392\u597d\u5e8f<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"828\" height=\"418\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-41.png\" alt=\"\" class=\"wp-image-204\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-41.png 828w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-41-300x151.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-41-768x388.png 768w\" sizes=\"auto, (max-width: 828px) 100vw, 828px\" \/><\/figure>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void shellsort(vector<int>& t) {\n\tint gap = t.size();\n\twhile (gap > 1) {\n\t\tgap = gap \/ 3 + 1;\n\t\tfor (int i = 0; i < t.size() - gap; i++) {\n\t\t\tint end = i;\n\t\t\twhile (end + gap <= t.size() - 1) {\n\t\t\t\tif (t[end] >= t[end + gap]) {\n\t\t\t\t\tswap(t[end], t[end + gap]);\n\t\t\t\t\tend += gap;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\u5e0c\u5c14\u6392\u5e8f\u7684\u7279\u6027\u603b\u7ed3\uff1a\n1. \u5e0c\u5c14\u6392\u5e8f\u662f\u5bf9\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u7684\u4f18\u5316\u3002\n2. \u5f53gap > 1\u65f6\u90fd\u662f\u9884\u6392\u5e8f\uff0c\u76ee\u7684\u662f\u8ba9\u6570\u7ec4\u66f4\u63a5\u8fd1\u4e8e\u6709\u5e8f\u3002\u5f53gap == 1\u65f6\uff0c\u6570\u7ec4\u5df2\u7ecf\u63a5\u8fd1\u6709\u5e8f\u7684\u4e86\uff0c\u8fd9\u6837\u5c31\n\u4f1a\u5f88\u5feb\u3002\u8fd9\u6837\u6574\u4f53\u800c\u8a00\uff0c\u53ef\u4ee5\u8fbe\u5230\u4f18\u5316\u7684\u6548\u679c\u3002\u6211\u4eec\u5b9e\u73b0\u540e\u53ef\u4ee5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u7684\u5bf9\u6bd4\u3002\n3. \u5e0c\u5c14\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u597d\u8ba1\u7b97\uff0c\u56e0\u4e3agap\u7684\u53d6\u503c\u65b9\u6cd5\u5f88\u591a\uff0c\u5bfc\u81f4\u5f88\u96be\u53bb\u8ba1\u7b97\uff0c\u56e0\u6b64\u5728\u597d\u4e9b\u6811\u4e2d\u7ed9\u51fa\u7684\n\u5e0c\u5c14\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e0d\u56fa\u5b9a\uff1a\u4e00\u822c\u8ba4\u4e3a\u4e3aN^1.3\u5de6\u53f3<\/code><\/pre>\n\n\n\n<p>\u5806\u6392\u5e8f\uff1a\u5728\u4e0a\u7bc7\u6587\u7ae0\u5df2\u4ecb\u7ecd<a href=\"http:\/\/www.tgwttt.xyz\/?p=194\">http:\/\/www.tgwttt.xyz\/?p=194<\/a>\u8fd9\u91cc\u4e0d\u505a\u8fc7\u591a\u5206\u6790<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5806\u6392\u5e8f\nvoid adjustdown(vector<int>& t, int number, int n) {\n\tint maxchild = number * 2 + 1;\n\twhile (maxchild < n) {\n\t\tif (maxchild + 1 < n &#038;&#038; t[maxchild] < t[maxchild + 1]) {\n\t\t\tmaxchild++;\n\t\t}\n\t\tif (t[number] < t[maxchild]) {\n\t\t\tswap(t[number], t[maxchild]);\n\t\t\tnumber = maxchild;\n\t\t\tmaxchild = number * 2 + 1;\n\t\t}\n\t\telse {\n\t\t\tbreak;\n\t\t}\n\t}\n}\nvoid heapsort(vector<int>&t) {\n\tfor(int i = (t.size() - 1 - 1) \/ 2; i >=0; i--) {\n\t\tadjustdown(t, i,t.size());\n\t}\n\tint end = t.size() - 1;\n\twhile (end>0) {\n\t\tswap(t[end], t[0]);\n\t\tadjustdown(t, 0, end);\n\t\tend--;\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u6700\u540e\u662f\u9009\u62e9\u6392\u5e8f\uff1a\u6bcf\u4e00\u6b21\u4ece\u5f85\u6392\u5e8f\u7684\u6570\u636e\u5143\u7d20\u4e2d\u9009\u51fa\u6700\u5c0f\uff08\u6216\u6700\u5927\uff09\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5b58\u653e\u5728\u5e8f\u5217\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u76f4\u5230\u5168\u90e8\u5f85\u6392\u5e8f\u7684\u6570\u636e\u5143\u7d20\u6392\u5b8c<br>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void selectsort(vector<int>&t) {\n\tint begin = 0;\n\tint end = t.size() - 1;\n\twhile (begin < end) {\n\t\tint min = begin;\n\t\tint max = end;\n\t\tfor (int i = begin; i <= end; i++) {\n\t\t\tif (t[i] <= t[min]) { min = i; }\n\t\t\tif (t[i] >= t[max]) { max = i; }\n\t\t}\n\t\tif (max + min == t.size()-1) {\n\t\t\tswap(t[min], t[max]);\n\t\t}\n\t\telse {\n\t\t\tswap(t[min], t[begin]);\n\t\t\tswap(t[max], t[end]);\n\t\t}\n\t\tcout << \"\u7b2ci\u8d9f\";\n\t\tfor (auto i : t) {\n\t\t\tcout << i << \" \";\n\t\t}\n\t\tcout << endl;\n\t\tbegin++;\n\t\tend--;\n\t}\n}\n\n\u76f4\u63a5\u9009\u62e9\u6392\u5e8f\u7684\u7279\u6027\u603b\u7ed3\uff1a\n1. \u76f4\u63a5\u9009\u62e9\u6392\u5e8f\u601d\u8003\u975e\u5e38\u597d\u7406\u89e3\uff0c\u4f46\u662f\u6548\u7387\u4e0d\u662f\u5f88\u597d\u3002\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\n2. \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N^2)\n3. \u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)\n4. \u7a33\u5b9a\u6027\uff1a\u4e0d\u7a33\u5b9a\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6392\u5e8f\u7684\u6982\u5ff5\u53ca\u5176\u8fd0\u7528 \u6392\u5e8f\u7684\u6982\u5ff5\u6392\u5e8f\uff1a\u6240\u8c13\u6392\u5e8f\uff0c\u5c31\u662f\u4f7f\u4e00\u4e32\u8bb0\u5f55\uff0c\u6309\u7167\u5176\u4e2d\u7684\u67d0\u4e2a\u6216\u67d0\u4e9b\u5173\u952e\u5b57\u7684\u5927\u5c0f\uff0c\u9012\u589e\u6216\u9012\u51cf&#8230;<\/p>\n","protected":false},"author":1,"featured_media":201,"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":[1],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/200","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=200"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions\/865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/201"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}