{"id":65901,"date":"2025-05-03T08:16:57","date_gmt":"2025-05-03T00:16:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65901\/"},"modified":"2025-05-03T08:16:57","modified_gmt":"2025-05-03T00:16:57","slug":"java%e4%b8%ad%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65901\/","title":{"rendered":"java\u4e2d\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\uff1aarrays.sort() \u65b9\u6cd5\uff1a \u4f7f\u7528\u5feb\u901f\u6392\u5e8f\uff0c\u7b80\u5355\u9ad8\u6548\u3002collections.sort() \u65b9\u6cd5\uff1a \u7528\u4e8e\u5305\u542b\u53ef\u6bd4\u8f83\u5143\u7d20\u7684\u6570\u7ec4\u3002\u5192\u6ce1\u6392\u5e8f\uff1a \u901a\u8fc7\u591a\u6b21\u6bd4\u8f83\u548c\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6392\u5e8f\u3002\u9009\u62e9\u6392\u5e8f\uff1a \u627e\u5230\u6700\u5c0f\u5143\u7d20\u5e76\u4e0e\u5f53\u524d\u5143\u7d20\u4ea4\u6362\u4f4d\u7f6e\u3002\u63d2\u5165\u6392\u5e8f\uff1a \u9010\u4e2a\u63d2\u5165\u5143\u7d20\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111315002139468.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u4e2d\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\u7684\u6700\u7b80\u5355\u4e14\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Arrays;\n\nint[] arr = {5, 3, 1, 2, 4};\nArrays.sort(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Collections.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u5bf9\u5305\u542b\u53ef\u6bd4\u8f83\u5143\u7d20\uff08\u5b9e\u73b0 Comparable \u63a5\u53e3\uff09\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>import java.util.Collections;\n\nInteger[] arr = {5, 3, 1, 2, 4};\nCollections.sort(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u76f8\u90bb\u5143\u7d20\u8fdb\u884c\u591a\u6b21\u6bd4\u8f83\u548c\u4ea4\u6362\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>public static void bubbleSort(int[] arr) {\n    int n = arr.length;\n    for (int i = 0; i &lt; n - 1; i++) {\n        for (int j = 0; j &lt; n - 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    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u9009\u62e9\u6392\u5e8f<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u67e5\u627e\u6700\u5c0f\u5143\u7d20\u5e76\u5728\u6570\u7ec4\u4e2d\u4ea4\u6362\u5176\u4f4d\u7f6e\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>public static void selectionSort(int[] arr) {\n    int n = arr.length;\n    for (int i = 0; i &lt; n - 1; i++) {\n        int minIdx = i;\n        for (int j = i + 1; j &lt; n; j++) {\n            if (arr[j] &lt; arr[minIdx]) {\n                minIdx = j;\n            }\n        }\n        int temp = arr[minIdx];\n        arr[minIdx] = arr[i];\n        arr[i] = temp;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u63d2\u5165\u6392\u5e8f<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u4e00\u6b21\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>public static void insertionSort(int[] arr) {\n    int n = arr.length;\n    for (int i = 1; i &lt; n; i++) {\n        int key = arr[i];\n        int j = i - 1;\n        while (j &gt;= 0 &amp;&amp; arr[j] &gt; key) {\n            arr[j + 1] = arr[j];\n            j--;\n        }\n        arr[j + 1] = key;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\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>java \u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\uff1aarrays.sort() \u65b9\u6cd5\uff1a \u4f7f\u7528\u5feb\u901f\u6392\u5e8f\uff0c\u7b80\u5355\u9ad8\u6548\u3002collections.sort() \u65b9\u6cd5\uff1a \u7528\u4e8e\u5305\u542b\u53ef\u6bd4\u8f83\u5143\u7d20\u7684\u6570\u7ec4\u3002\u5192\u6ce1\u6392\u5e8f\uff1a \u901a\u8fc7\u591a\u6b21\u6bd4\u8f83\u548c\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6392\u5e8f\u3002\u9009\u62e9\u6392\u5e8f\uff1a \u627e\u5230\u6700\u5c0f\u5143\u7d20\u5e76\u4e0e\u5f53\u524d\u5143\u7d20\u4ea4\u6362\u4f4d\u7f6e\u3002\u63d2\u5165\u6392\u5e8f\uff1a \u9010\u4e2a\u63d2\u5165\u5143\u7d20\u6392\u5e8f\u3002 Java \u4e2d\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f \u5728 Java \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5 \u8fd9\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\u7684\u6700\u7b80\u5355\u4e14\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.Arrays; int[] arr = {5, 3, 1, 2, 4}; Arrays.sort(arr); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Collections.sort() \u65b9\u6cd5 \u6b64\u65b9\u6cd5\u7528\u4e8e\u5bf9\u5305\u542b\u53ef\u6bd4\u8f83\u5143\u7d20\uff08\u5b9e\u73b0 Comparable \u63a5\u53e3\uff09\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 import java.util.Collections; Integer[] arr = {5, 3, 1, 2, 4}; Collections.sort(arr); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 \u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u76f8\u90bb\u5143\u7d20\u8fdb\u884c\u591a\u6b21\u6bd4\u8f83\u548c\u4ea4\u6362\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 public static void bubbleSort(int[] [&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-65901","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65901","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=65901"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65901\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}