{"id":5000,"date":"2023-11-13T23:49:18","date_gmt":"2023-11-13T15:49:18","guid":{"rendered":"https:\/\/white-night.club\/?p=5000"},"modified":"2023-11-14T00:25:52","modified_gmt":"2023-11-13T16:25:52","slug":"ad149","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/13\/ad149\/","title":{"rendered":"\u5192\u6ce1\u6392\u5e8f"},"content":{"rendered":"\n<p>\u8981\u662f\u54ea\u6b21\u6211\u5192\u6ce1\u6392\u5e8f\u6ca1\u80fd\u5199\u51fa\u6765\uff0c\u76f4\u63a5\u7ed9\u81ea\u5df1\u4e00\u5df4\u638c\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-f587f1a229f544d3a965cc02d3e73007\">\n<p>\u5192\u6ce1\u6392\u5e8f<\/p>\n<cite><em>\u4e24\u4e24\u4ea4\u6362<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5192\u6ce1\u6392\u5e8f\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u53cc\u91cd\u5faa\u73af\u3002\u6709n\u4e2a\u5143\u7d20\u5c31\u9700\u8981\u8fdb\u884cn\u6b21\u6392\u5e8f\u3002\u82e5\u6709n\u4e2a\u5143\u7d20\uff0c\u5916\u5c42\u5faa\u73af\u8868\u793a\u9700\u8981\u8fdb\u884cn-1=i\u6b21\u6392\u5e8f\uff0c\u5185\u5c42\u5faa\u73af\u5219\u8868\u793a\u4ece\u7b2c0\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u9700\u8981\u8fdb\u884cn-1-i\u6b21\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6309\u4ece\u5c0f\u5230\u5927\u5f00\u59cb\u6392\u5e8f\u3002\u5047\u8bbe\u4e00\u5f00\u59cb\u6700\u5927\u503c\u5c31\u5728\u5f00\u5934\u3002\u5148\u5c06nums[0]\u548cnums[1]\u8fdb\u884c\u6bd4\u8f83\uff0c\u6b64\u65f6nums[0]\u66f4\u5927\uff0c\u6240\u4ee5\u548cnums[1]\u8fdb\u884c\u4ea4\u6362\u3002\u518d\u5c06nums[1]\u548cnums[2]\u8fdb\u884c\u6bd4\u8f83\u548c\u4ea4\u6362\u3002\u4ee5\u6b64\u7c7b\u63a8\u76f4\u5230\u6700\u5f00\u59cb\u7684nums[0]\u627e\u4e0d\u5230\u6bd4\u5b83\u66f4\u5927\u7684\u503c\u3002\u90a3\u4e48\u6b64\u65f6nums[0]\u5df2\u7ecf\u88ab\u6392\u5230\u4e86\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/p>\n\n\n\n<p>\u6b64\u65f6\u5728\u5bf9\u5269\u4e0b\u7684n-1\uff0cn-2\u4e2a\u5143\u7d20\u8fdb\u884c\u5192\u6ce1\u6392\u5e8f\uff0c\u76f4\u5230\u6ca1\u6709\u53ef\u6392\u5e8f\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e09\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u4ece\u5927\u5230\u5c0f\u6392\u5e8f.\u4ee5\u4e0b\u662f\u6bcf\u6b21\u6267\u884c\u4ea4\u6362\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<ul>\n<li>1\uff0c2\uff0c3<\/li>\n\n\n\n<li>2\uff0c1\uff0c3<\/li>\n\n\n\n<li>2\uff0c3\uff0c1<\/li>\n\n\n\n<li>3\uff0c2\uff0c1<\/li>\n<\/ul>\n\n\n\n<p>\u4e3a\u5565\u53eb\u5b83\u5192\u6ce1\u6392\u5e8f\u5462\uff1f\u56e0\u4e3a\u5982\u679c\u662f\u6309\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u5927\u7684\u503c\u4f1a\u5728\u6bcf\u6b21\u4ea4\u6362\u7684\u8fc7\u7a0b\u4e2d\u88ab\u6162\u6162\u7684\u4ea4\u6362\u5230\u6570\u7ec4\u5c3e\u90e8\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u5f88\u660e\u663e\uff0c\u5192\u6ce1\u6392\u5e8f\u6700\u5dee\u7684\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230O(n<sup>2<\/sup>)\u3002\u56e0\u4e3a\u6b64\u65f6\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u9700\u8981\u7ecf\u8fc7\u5185\u90e8\u5faa\u73af\u7684IF\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-cde653f196b866d75be332d89678e98e\">\n<p>\u5404\u8bed\u8a00\u5b9e\u73b0<\/p>\n<cite><em>C++\u548cGO<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u7528C++\u548cGO\u6765\u5b9e\u73b0\u3002\u7528C++\u662f\u56e0\u4e3a\u5e38\u7528\u3002\u7528GO\u662f\u56e0\u4e3a\u76ee\u524d\u6b63\u5728\u5b66GO\u3002<\/p>\n\n\n\n<p>\u5e38\u89c4\u7684\u4ea4\u6362\u76f4\u63a5\u5b9a\u4e49\u4e00\u4e2a\u65b0\u53d8\u91cf\u5c31\u884c\uff0c\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u7528\u5f02\u6216\u6765\u4ea4\u6362\u3002\u53cd\u6b63\u601d\u8def\u662f\u4e00\u6837\u7684\u3002\u8fd9\u91cc\u5206\u522b\u7528\u5e38\u89c4\u65b9\u6cd5\uff08C++\uff09\u548c\u5f02\u6216\u65b9\u6cd5\uff08GO\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>C++\uff08\u5e38\u89c4\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{\n\tint nums&#91;10] = { 0,1,2,3,4,5,6,7,8,9};\n\tint size = 10;\n\tfor (int i = 0; i &lt; size-1; i++)\n\t{\n\t\tfor (int j = 0; j &lt; size-1-i; j++)\n\t\t{\n\t\t\tif (nums&#91;j+1] &gt; nums&#91;j])\n\t\t\t{\n\t\t\t\tint tmp = nums&#91;j];\n\t\t\t\tnums&#91;j] = nums&#91;j + 1];\n\t\t\t\tnums&#91;j + 1] = tmp;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i &lt; 10; i++)\n\t{\n\t\tcout &lt;&lt; nums&#91;i] &lt;&lt; \" \";\n\t}\n\tcout &lt;&lt; endl;\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><strong>GO\uff08\u5f02\u6216\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n\tnums := &#91;]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n\tvar size = 10\n\tfor i := 0; i &lt; size-1; i++ {\n\t\tfor j := 0; j &lt; size-1-i; j++ {\n\t\t\tif nums&#91;j+1] &gt; nums&#91;j] {\n\t\t\t\tnums&#91;j] = nums&#91;j] ^ nums&#91;j+1]\n\t\t\t\tnums&#91;j+1] = nums&#91;j] ^ nums&#91;j+1]\n\t\t\t\tnums&#91;j] = nums&#91;j] ^ nums&#91;j+1]\n\t\t\t}\n\t\t}\n\t}\n\tfmt.Printf(\"Sorted array is %d\", nums)\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u57fa\u672c\u7684\u6392\u5e8f\u65b9\u6cd5\u4e4b\u4e00<\/p>\n","protected":false},"author":1,"featured_media":3220,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[25,50],"tags":[36],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5000"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=5000"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5000\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/3220"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=5000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=5000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=5000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}