{"id":36723,"date":"2024-11-26T14:27:26","date_gmt":"2024-11-26T06:27:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36723\/"},"modified":"2024-11-26T14:27:26","modified_gmt":"2024-11-26T06:27:26","slug":"%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%8e%92%e5%ba%8fjava","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36723\/","title":{"rendered":"\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava"},"content":{"rendered":"<blockquote><p>\n  java\u4e2d\u7684\u6570\u7ec4\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684arrays.sort()\u65b9\u6cd5\u5b9e\u73b0\u3002\u57fa\u672c\u6392\u5e8f\u7b97\u6cd5\u5305\u62ec\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u3002\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111214492660017.jpg\" class=\"aligncenter\" title=\"\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava\u63d2\u56fe\" alt=\"\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6570\u7ec4\u6392\u5e8f\u5728 Java \u4e2d\u7684\u5b9e\u73b0<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><br \/>\u5728 Java \u4e2d\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u6392\u5e8f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Java \u4e2d\u5e38\u7528\u7684\u6570\u7ec4\u6392\u5e8f\u7b97\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5185\u7f6e\u6392\u5e8f\u65b9\u6cd5<\/strong><br \/>Java \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 Arrays.sort() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>\/\/ \u5bf9 int \u6570\u7ec4\u6392\u5e8f\nint[] arr = {3, 1, 5, 2, 4};\nArrays.sort(arr); \/\/ {1, 2, 3, 4, 5}\n\n\/\/ \u5bf9 String \u6570\u7ec4\u6392\u5e8f\nString[] arr = {\"John\", \"Alice\", \"Bob\", \"David\"};\nArrays.sort(arr); \/\/ {\"Alice\", \"Bob\", \"David\", \"John\"}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u57fa\u672c\u6392\u5e8f\u7b97\u6cd5<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u65b9\u6cd5\u4e4b\u5916\uff0cJava \u4e2d\u8fd8\u6709\u8bb8\u591a\u57fa\u672c\u6392\u5e8f\u7b97\u6cd5\u53ef\u4f9b\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6700\u5e38\u89c1\u7684\u7b97\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. <\/strong><br \/>\u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4e0d\u65ad\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5e76\u4ea4\u6362\u5b83\u4eec\u6765\u5c06\u5143\u7d20\u79fb\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/p>\n<pre>public static void bubbleSort(int[] arr) {\n  for (int i = 0; i &lt; arr.length; i++) {\n    for (int j = 1; j &lt; arr.length - i; j++) {\n      if (arr[j - 1] &gt; arr[j]) {\n        int temp = arr[j - 1];\n        arr[j - 1] = arr[j];\n        arr[j] = temp;\n      }\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u9009\u62e9\u6392\u5e8f<\/strong><br \/>\u9009\u62e9\u6392\u5e8f\u901a\u8fc7\u6bcf\u6b21\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5c0f\uff08\u6216\u6700\u5927\uff09\u7684\u5143\u7d20\u5e76\u5c06\u5176\u653e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u6765\u5de5\u4f5c\u3002<\/p>\n<pre>public static void selectionSort(int[] arr) {\n  for (int i = 0; i &lt; arr.length; i++) {\n    int minIndex = i;\n    for (int j = i + 1; j &lt; arr.length; j++) {\n      if (arr[j] &lt; arr[minIndex]) {\n        minIndex = j;\n      }\n    }\n    int temp = arr[i];\n    arr[i] = arr[minIndex];\n    arr[minIndex] = temp;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u63d2\u5165\u6392\u5e8f<\/strong><br \/>\u63d2\u5165\u6392\u5e8f\u901a\u8fc7\u5c06\u6bcf\u4e2a\u5143\u7d20\u63d2\u5165\u5230\u5b83\u524d\u9762\u7684\u5df2\u6392\u5e8f\u5b50\u6570\u7ec4\u4e2d\u6765\u5de5\u4f5c\u3002<\/p>\n<pre>public static void insertionSort(int[] arr) {\n  for (int i = 1; i &lt; arr.length; i++) {\n    int current = arr[i];\n    int j = i - 1;\n    while (j &gt;= 0 &amp;&amp; current &lt; arr[j]) {\n      arr[j + 1] = arr[j];\n      j--;\n    }\n    arr[j + 1] = current;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f<\/strong><br \/>\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0cJava \u5141\u8bb8\u60a8\u5b9a\u4e49\u81ea\u5df1\u7684\u6bd4\u8f83\u5668\u6765\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u3002\u6bd4\u8f83\u5668\u662f\u4e00\u4e2a\u7c7b\uff0c\u5b83\u5b9e\u73b0 Comparator \u63a5\u53e3\u5e76\u91cd\u5199 compare() \u65b9\u6cd5\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n<pre>\/\/ \u6bd4\u8f83\u5668\u7528\u4e8e\u6bd4\u8f83\u5b66\u751f\u7684\u59d3\u540d\npublic class StudentNameComparator implements Comparator&lt;Student&gt; {\n  @Override\n  public int compare(Student s1, Student s2) {\n    return s1.getName().compareTo(s2.getName());\n  }\n}\n\n\/\/ \u4f7f\u7528\u6bd4\u8f83\u5668\u5bf9\u5b66\u751f\u6570\u7ec4\u6392\u5e8f\nStudent[] students = {new Student(\"Alice\", 21), new Student(\"Bob\", 20)};\nArrays.sort(students, new StudentNameComparator());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava\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>java\u4e2d\u7684\u6570\u7ec4\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684arrays.sort()\u65b9\u6cd5\u5b9e\u73b0\u3002\u57fa\u672c\u6392\u5e8f\u7b97\u6cd5\u5305\u62ec\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u3002\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u3002 \u6570\u7ec4\u6392\u5e8f\u5728 Java \u4e2d\u7684\u5b9e\u73b0 \u524d\u8a00\u5728 Java \u4e2d\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u6392\u5e8f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Java \u4e2d\u5e38\u7528\u7684\u6570\u7ec4\u6392\u5e8f\u7b97\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002 \u5185\u7f6e\u6392\u5e8f\u65b9\u6cd5Java \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 Arrays.sort() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \/\/ \u5bf9 int \u6570\u7ec4\u6392\u5e8f int[] arr = {3, 1, 5, 2, 4}; Arrays.sort(arr); \/\/ {1, 2, 3, 4, 5} \/\/ \u5bf9 String \u6570\u7ec4\u6392\u5e8f String[] arr = {&#8220;John&#8221;, &#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;David&#8221;}; Arrays.sort(arr); \/\/ {&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;David&#8221;, &#8220;John&#8221;} \u767b\u5f55\u540e\u590d\u5236 \u57fa\u672c\u6392\u5e8f\u7b97\u6cd5\u9664\u4e86\u5185\u7f6e\u65b9\u6cd5\u4e4b\u5916\uff0cJava \u4e2d\u8fd8\u6709\u8bb8\u591a\u57fa\u672c\u6392\u5e8f\u7b97\u6cd5\u53ef\u4f9b\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6700\u5e38\u89c1\u7684\u7b97\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4e0d\u65ad\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5e76\u4ea4\u6362\u5b83\u4eec\u6765\u5c06\u5143\u7d20\u79fb\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\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-36723","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36723","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=36723"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36723\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}