{"id":66542,"date":"2025-05-03T13:05:52","date_gmt":"2025-05-03T05:05:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66542\/"},"modified":"2025-05-03T13:05:52","modified_gmt":"2025-05-03T05:05:52","slug":"java%e4%bb%a3%e7%a0%81%e6%80%8e%e4%b9%88%e7%bb%99%e6%95%b0%e7%bb%84%e5%86%85%e5%85%83%e7%b4%a0%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66542\/","title":{"rendered":"java\u4ee3\u7801\u600e\u4e48\u7ed9\u6570\u7ec4\u5185\u5143\u7d20\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u6392\u5e8f\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u5192\u6ce1\u6392\u5e8f\uff1a\u9002\u7528\u4e8e\u5c0f\u6570\u7ec4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n^2)\u3002\u5feb\u901f\u6392\u5e8f\uff1a\u6700\u4f18\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\uff0c\u5e73\u5747\u60c5\u51b5\u4e0b\u4e3a o(n^2)\u3002\u5f52\u5e76\u6392\u5e8f\uff1a\u7a33\u5b9a\u6392\u5e8f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002arrays.sort()\uff1a\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111606032564031.jpg\" class=\"aligncenter\" title=\"java\u4ee3\u7801\u600e\u4e48\u7ed9\u6570\u7ec4\u5185\u5143\u7d20\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u4ee3\u7801\u600e\u4e48\u7ed9\u6570\u7ec4\u5185\u5143\u7d20\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u6700\u5e38\u89c1\u7684\u6392\u5e8f\u7b97\u6cd5\u53ca\u5176\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<p><strong>1. <\/strong><\/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>2. \u5feb\u901f\u6392\u5e8f<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public static void quickSort(int[] arr, int low, int high) {\n    if (low &lt; high) {\n        int partitionIndex = partition(arr, low, high);\n\n        quickSort(arr, low, partitionIndex - 1);\n        quickSort(arr, partitionIndex + 1, high);\n    }\n}\n\nprivate static int partition(int[] arr, int low, int high) {\n    int pivot = arr[high];\n    int i = (low - 1);\n\n    for (int j = low; j &lt; high; j++) {\n        if (arr[j] &lt; pivot) {\n            i++;\n\n            int temp = arr[i];\n            arr[i] = arr[j];\n            arr[j] = temp;\n        }\n    }\n\n    int temp = arr[i + 1];\n    arr[i + 1] = arr[high];\n    arr[high] = temp;\n\n    return (i + 1);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5f52\u5e76\u6392\u5e8f<\/strong><\/p>\n<pre>public static void mergeSort(int[] arr, int[] temp, int left, int right) {\n    if (left &lt; right) {\n        int mid = (left + right) \/ 2;\n\n        mergeSort(arr, temp, left, mid);\n        mergeSort(arr, temp, mid + 1, right);\n\n        merge(arr, temp, left, mid, right);\n    }\n}\n\nprivate static void merge(int[] arr, int[] temp, int left, int mid, int right) {\n    int i = left;\n    int j = mid + 1;\n    int k = left;\n\n    while (i &lt;= mid &amp;&amp; j &lt;= right) {\n        if (arr[i] &lt;= arr[j]) {\n            temp[k] = arr[i];\n            i++;\n        } else {\n            temp[k] = arr[j];\n            j++;\n        }\n        k++;\n    }\n\n    while (i &lt;= mid) {\n        temp[k] = arr[i];\n        i++;\n        k++;\n    }\n\n    while (j &lt;= right) {\n        temp[k] = arr[j];\n        j++;\n        k++;\n    }\n\n    for (i = left; i &lt;= right; i++) {\n        arr[i] = temp[i];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. Arrays.sort()<\/strong><\/p>\n<p>Java \u8fd8\u63d0\u4f9b\u4e86 Arrays.sort() \u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u6765\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>Arrays.sort(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u56db\u79cd\u6392\u5e8f\u7b97\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u6570\u7ec4\u5927\u5c0f\u548c\u5143\u7d20\u5206\u5e03\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u53ef\u4ee5\u4f18\u5316\u6392\u5e8f\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4ee3\u7801\u600e\u4e48\u7ed9\u6570\u7ec4\u5185\u5143\u7d20\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 \u6392\u5e8f\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u5192\u6ce1\u6392\u5e8f\uff1a\u9002\u7528\u4e8e\u5c0f\u6570\u7ec4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n^2)\u3002\u5feb\u901f\u6392\u5e8f\uff1a\u6700\u4f18\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\uff0c\u5e73\u5747\u60c5\u51b5\u4e0b\u4e3a o(n^2)\u3002\u5f52\u5e76\u6392\u5e8f\uff1a\u7a33\u5b9a\u6392\u5e8f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002arrays.sort()\uff1a\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002 \u5982\u4f55\u5bf9 Java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u6700\u5e38\u89c1\u7684\u6392\u5e8f\u7b97\u6cd5\u53ca\u5176\u5b9e\u73b0\u65b9\u5f0f\uff1a 1. public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i &lt; n &#8211; 1; i++) { for (int j = 0; j &lt; n &#8211; i &#8211; 1; j++) { [&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-66542","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66542","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=66542"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66542\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}