{"id":37746,"date":"2024-11-26T09:08:50","date_gmt":"2024-11-26T01:08:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37746\/"},"modified":"2024-11-26T09:08:50","modified_gmt":"2024-11-26T01:08:50","slug":"%e5%86%92%e6%b3%a1%e6%8e%92%e5%ba%8fjava%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37746\/","title":{"rendered":"\u5192\u6ce1\u6392\u5e8fjava\u6570\u7ec4\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\u662f\u4e00\u79cd\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u9010\u6b21\u4ea4\u6362\u6700\u5927\u5143\u7d20\u5230\u6570\u7ec4\u672b\u5c3e\u6765\u5bf9\u6570\u7ec4\u6392\u5e8f\u3002\u7b97\u6cd5\u6b65\u9aa4\uff1a1. \u5916\u5faa\u73af\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff1b2. \u5185\u5faa\u73af\u5c06\u5f53\u524d\u5143\u7d20\u4e0e\u540e\u4e00\u4e2a\u5143\u7d20\u6bd4\u8f83\u5e76\u4ea4\u6362\uff1b3. \u91cd\u590d\u6b65\u9aa4\u76f4\u5230\u6ca1\u6709\u4ea4\u6362\u53d1\u751f\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n^2)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a o(1)\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111309460434057.jpg\" class=\"aligncenter\" title=\"\u5192\u6ce1\u6392\u5e8fjava\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"\u5192\u6ce1\u6392\u5e8fjava\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong> Java \u6570\u7ec4<\/strong><\/p>\n<p><strong>\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5<\/strong><\/p>\n<p>\u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u662f\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u591a\u8d9f\u626b\u63cf\uff0c\u6bcf\u6b21\u626b\u63cf\u5c06\u6700\u5927\u7684\u5143\u7d20\u9010\u4e2a\u4ea4\u6362\u5230\u6570\u7ec4\u672b\u5c3e\u3002<\/p>\n<p><strong>Java \u4ee3\u7801<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0b Java \u4ee3\u7801\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\uff1a<\/p>\n<pre>public static void bubbleSort(int[] arr) {\n    for (int i = 0; i &lt; arr.length - 1; i++) {\n        for (int j = 0; j &lt; arr.length - i - 1; j++) {\n            if (arr[j] &gt; arr[j + 1]) {\n                \/\/ \u4ea4\u6362 arr[j] \u548c arr[j+1]\n                int temp = arr[j];\n                arr[j] = arr[j + 1];\n                arr[j + 1] = temp;\n            }\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b97\u6cd5\u6b65\u9aa4<\/strong><\/p>\n<ul>\n<li>\u5916\u5faa\u73af i \u904d\u5386\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<li>\u5185\u5faa\u73af j \u5c06\u5f53\u524d\u5143\u7d20\u4e0e\u540e\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\u3002<\/li>\n<li>\u91cd\u590d\u6b64\u8fc7\u7a0b\uff0c\u76f4\u5230\u6ca1\u6709\u66f4\u591a\u4ea4\u6362\u9700\u8981\u8fdb\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong><\/p>\n<p>\u5192\u6ce1\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\uff0c\u5176\u4e2d n \u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u591a\u6b21\u904d\u5386\u6570\u7ec4\u624d\u80fd\u6392\u5e8f\u5b8c\u6210\u3002<\/p>\n<p><strong>\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong><\/p>\n<p>\u5192\u6ce1\u6392\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\uff0c\u56e0\u4e3a\u7b97\u6cd5\u4e0d\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b8c\u6210\u6392\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5199\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\u662f\u4e00\u79cd\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u9010\u6b21\u4ea4\u6362\u6700\u5927\u5143\u7d20\u5230\u6570\u7ec4\u672b\u5c3e\u6765\u5bf9\u6570\u7ec4\u6392\u5e8f\u3002\u7b97\u6cd5\u6b65\u9aa4\uff1a1. \u5916\u5faa\u73af\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff1b2. \u5185\u5faa\u73af\u5c06\u5f53\u524d\u5143\u7d20\u4e0e\u540e\u4e00\u4e2a\u5143\u7d20\u6bd4\u8f83\u5e76\u4ea4\u6362\uff1b3. \u91cd\u590d\u6b65\u9aa4\u76f4\u5230\u6ca1\u6709\u4ea4\u6362\u53d1\u751f\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n^2)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a o(1)\u3002 Java \u6570\u7ec4 \u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5 \u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u662f\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u591a\u8d9f\u626b\u63cf\uff0c\u6bcf\u6b21\u626b\u63cf\u5c06\u6700\u5927\u7684\u5143\u7d20\u9010\u4e2a\u4ea4\u6362\u5230\u6570\u7ec4\u672b\u5c3e\u3002 Java \u4ee3\u7801 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4ee5\u4e0b Java \u4ee3\u7801\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\uff1a public static void bubbleSort(int[] arr) { for (int i = 0; i &lt; arr.length &#8211; 1; i++) { for (int j = 0; j &lt; arr.length &#8211; i &#8211; 1; j++) { if (arr[j] &gt; arr[j + 1]) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-37746","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=37746"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37746\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}