{"id":66961,"date":"2025-05-03T12:01:18","date_gmt":"2025-05-03T04:01:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66961\/"},"modified":"2025-05-03T12:01:18","modified_gmt":"2025-05-03T04:01:18","slug":"java%e6%80%8e%e4%b9%88%e5%af%b9%e4%b8%80%e4%bd%8d%e6%95%b0%e7%bb%84%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66961\/","title":{"rendered":"java\u600e\u4e48\u5bf9\u4e00\u4f4d\u6570\u7ec4\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  \u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\uff0c\u7b97\u6cd5\u4e3a\u5feb\u901f\u6392\u5e8f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002\u4f7f\u7528 collections.sort() \u65b9\u6cd5\uff0c\u7b97\u6cd5\u4e3a\u5f52\u5e76\u6392\u5e8f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a o(n log n)\u3002\u5bf9\u4e8e\u5c0f\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528\u624b\u52a8\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982\u5192\u6ce1\u6392\u5e8f\u6216\u9009\u62e9\u6392\u5e8f\uff0c\u6548\u7387\u8f83\u4f4e\u4f46\u8db3\u591f\u5feb\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111720491741462.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5bf9\u4e00\u4f4d\u6570\u7ec4\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5bf9\u4e00\u4f4d\u6570\u7ec4\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n<p>\u4e00\u7ef4\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u5143\u7d20\u6309\u7ebf\u6027\u987a\u5e8f\u5b58\u50a8\u3002\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u610f\u5473\u7740\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5b89\u6392\u4e3a\u5347\u5e8f\u6216\u964d\u5e8f\u3002<\/p>\n<p><strong>Java \u4e2d\u5bf9\u4e00\u7ef4\u6570\u7ec4\u6392<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. Arrays.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8be5\u65b9\u6cd5\u662f Java \u4e2d\u7528\u4e8e\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u7684\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\uff0c\u8be5\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>int[] arr = {5, 3, 8, 1, 2};\nArrays.sort(arr);  \/\/ \u5bf9 arr \u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. Collections.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8be5\u65b9\u6cd5\u662f Java Collections Framework \u4e2d\u7528\u4e8e\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u7684\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u8be5\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a O(n log n)\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>Integer[] arr = {5, 3, 8, 1, 2};\nList&lt;Integer&gt; list = Arrays.asList(arr);\nCollections.sort(list);  \/\/ \u5bf9 arr \u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u624b\u52a8\u6392\u5e8f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u624b\u52a8\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982\u6216\u9009\u62e9\u6392\u5e8f\u3002\u8fd9\u4e9b\u7b97\u6cd5\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u4f46\u5bf9\u4e8e\u5c0f\u6570\u636e\u96c6\u6765\u8bf4\u8db3\u591f\u5feb\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>int[] arr = {5, 3, 8, 1, 2};\n\n\/\/ \u5192\u6ce1\u6392\u5e8f\nfor (int i = 0; i &lt; arr.length; i++) {\n    for (int j = 0; j &lt; arr.length - i - 1; j++) {\n        if (arr[j] &gt; arr[j + 1]) {\n            int temp = arr[j];\n            arr[j] = arr[j + 1];\n            arr[j + 1] = temp;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u6392\u5e8f<\/strong><\/p>\n<pre>int[] arr = {5, 3, 8, 1, 2};\n\n\/\/ \u9009\u62e9\u6392\u5e8f\nfor (int i = 0; i &lt; arr.length; i++) {\n    int minIdx = i;\n    for (int j = i + 1; j &lt; arr.length; j++) {\n        if (arr[j] &lt; arr[minIdx]) {\n            minIdx = j;\n        }\n    }\n\n    int temp = arr[minIdx];\n    arr[minIdx] = arr[i];\n    arr[i] = temp;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5bf9\u4e00\u4f4d\u6570\u7ec4\u6392\u5e8f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\uff0c\u7b97\u6cd5\u4e3a\u5feb\u901f\u6392\u5e8f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002\u4f7f\u7528 collections.sort() \u65b9\u6cd5\uff0c\u7b97\u6cd5\u4e3a\u5f52\u5e76\u6392\u5e8f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a o(n log n)\u3002\u5bf9\u4e8e\u5c0f\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528\u624b\u52a8\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982\u5192\u6ce1\u6392\u5e8f\u6216\u9009\u62e9\u6392\u5e8f\uff0c\u6548\u7387\u8f83\u4f4e\u4f46\u8db3\u591f\u5feb\u3002 \u5982\u4f55\u5bf9\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f \u4e00\u7ef4\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u5143\u7d20\u6309\u7ebf\u6027\u987a\u5e8f\u5b58\u50a8\u3002\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u610f\u5473\u7740\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5b89\u6392\u4e3a\u5347\u5e8f\u6216\u964d\u5e8f\u3002 Java \u4e2d\u5bf9\u4e00\u7ef4\u6570\u7ec4\u6392 Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. Arrays.sort() \u65b9\u6cd5 \u8be5\u65b9\u6cd5\u662f Java \u4e2d\u7528\u4e8e\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u7684\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\uff0c\u8be5\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u4f7f\u7528\u65b9\u6cd5\uff1a int[] arr = {5, 3, 8, 1, 2}; Arrays.sort(arr); \/\/ \u5bf9 arr \u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f \u767b\u5f55\u540e\u590d\u5236 2. Collections.sort() \u65b9\u6cd5 \u8be5\u65b9\u6cd5\u662f Java Collections Framework \u4e2d\u7528\u4e8e\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u7684\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u8be5\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a O(n log n)\u3002 [&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-66961","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66961","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=66961"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66961\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}