{"id":65924,"date":"2025-05-03T15:53:23","date_gmt":"2025-05-03T07:53:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65924\/"},"modified":"2025-05-03T15:53:23","modified_gmt":"2025-05-03T07:53:23","slug":"java%e6%80%8e%e4%b9%88%e6%b1%82%e6%95%b0%e7%bb%84%e7%9a%84%e4%b8%ad%e9%97%b4%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65924\/","title":{"rendered":"Java\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u4e2d\u95f4\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6c42\u6570\u7ec4\u4e2d\u95f4\u503c\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a1. \u4f7f\u7528\u6392\u5e8f\u540e\u53d6\u4e2d\uff0c\u5bf9\u6570\u7ec4\u6392\u5e8f\u540e\u53d6\u4e2d\u95f4\u7d22\u5f15\u5143\u7d20\uff1b2. \u4f7f\u7528\u5feb\u901f\u9009\u62e9\uff0c\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u90e8\u5206\uff0c\u627e\u5230\u6bd4\u57fa\u51c6\u5143\u7d20\u5927\u4e14\u5728\u524d\u534a\u90e8\u5206\uff08\u5947\u6570\u957f\u5ea6\uff09\u6216\u540e\u534a\u90e8\u5206\uff08\u5076\u6570\u957f\u5ea6\uff09\u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111316003264624.jpg\" class=\"aligncenter\" title=\"Java\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u4e2d\u95f4\u503c\u63d2\u56fe\" alt=\"Java\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u4e2d\u95f4\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6c42\u6570\u7ec4\u4e2d\u95f4\u503c\u7684\u4e24\u79cd\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u5f00\u95e8\u89c1\u5c71\uff1a<\/strong><\/p>\n<p>Java \u4e2d\u6c42\u6570\u7ec4\u4e2d\u95f4\u503c\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a<\/p>\n<p><strong>\u8be6\u7ec6\u5c55\u5f00\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u6392\u5e8f\u540e\u53d6\u4e2d<\/strong><\/p>\n<ol>\n<li>\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a\u5947\u6570\uff0c\u4e2d\u95f4\u503c\u4e3a\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a (n-1)\/2 \u7684\u5143\u7d20\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a\u5076\u6570\uff0c\u4e2d\u95f4\u503c\u4e3a\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a n\/2 \u548c n\/2-1 \u7684\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002<\/li>\n<\/ol>\n<pre>\/\/ \u6570\u7ec4 arr\nint[] arr = {1, 3, 5, 2, 4};\n\n\/\/ \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\nArrays.sort(arr);\n\n\/\/ \u6570\u7ec4\u957f\u5ea6\u4e3a\u5947\u6570\nif (arr.length % 2 != 0) {\n    int midIndex = (arr.length - 1) \/ 2;\n    int midValue = arr[midIndex];\n    System.out.println(\"\u4e2d\u95f4\u503c\u4e3a\uff1a\" + midValue);\n}\n\/\/ \u6570\u7ec4\u957f\u5ea6\u4e3a\u5076\u6570\nelse {\n    int midIndex1 = arr.length \/ 2;\n    int midIndex2 = midIndex1 - 1;\n    double midValue = (arr[midIndex1] + arr[midIndex2]) \/ 2.0;\n    System.out.println(\"\u4e2d\u95f4\u503c\u4e3a\uff1a\" + midValue);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528\u5feb\u901f\u9009\u62e9<\/strong><\/p>\n<ol>\n<li>\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\uff08\u901a\u5e38\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u3002<\/li>\n<li>\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5212\u5206\u4e3a\u6bd4\u57fa\u51c6\u5143\u7d20\u5c0f\u3001\u7b49\u4e8e\u548c\u5927\u7684\u4e09\u4e2a\u90e8\u5206\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a\u5947\u6570\uff0c\u4e2d\u95f4\u503c\u662f\u6bd4\u57fa\u51c6\u5143\u7d20\u5927\u4e14\u6570\u76ee\u6bd4\u4e00\u534a\u5c11\u7684\u5143\u7d20\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a\u5076\u6570\uff0c\u4e2d\u95f4\u503c\u662f\u6bd4\u57fa\u51c6\u5143\u7d20\u5927\u4e14\u6570\u76ee\u6bd4\u4e00\u534a\u591a\u6216\u7b49\u4e8e\u4e00\u534a\u7684\u5143\u7d20\u3002<\/li>\n<\/ol>\n<pre>\/\/ \u5feb\u901f\u9009\u62e9\u7b97\u6cd5\nint quickSelect(int[] arr, int low, int high, int k) {\n    \/\/ \u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\n    if (low == high) {\n        return arr[low];\n    }\n\n    \/\/ \u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\n    int pivot = arr[high];\n\n    \/\/ \u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u90e8\u5206\n    int partition = partition(arr, low, high, pivot);\n\n    \/\/ \u627e\u5230\u4e2d\u95f4\u503c\n    if (k == partition) {\n        return arr[partition];\n    } else if (k &lt; partition) {\n        return quickSelect(arr, low, partition - 1, k);\n    } else {\n        return quickSelect(arr, partition + 1, high, k);\n    }\n}\n\n\/\/ \u5212\u5206\u6570\u7ec4\u7684\u51fd\u6570\nint partition(int[] arr, int low, int high, int pivot) {\n    int i = low - 1;\n    for (int j = low; j &lt; high; j++) {\n        if (arr[j] &lt;= pivot) {\n            i++;\n            int temp = arr[i];\n            arr[i] = arr[j];\n            arr[j] = temp;\n        }\n    }\n    int temp = arr[i + 1];\n    arr[i + 1] = arr[high];\n    arr[high] = temp;\n    return i + 1;\n}\n\n\/\/ \u4e3b\u51fd\u6570\npublic static void main(String[] args) {\n    \/\/ \u6570\u7ec4 arr\n    int[] arr = {1, 3, 5, 2, 4};\n\n    \/\/ \u4f7f\u7528\u5feb\u901f\u9009\u62e9\u7b97\u6cd5\u83b7\u53d6\u4e2d\u95f4\u503c\n    int midValue = quickSelect(arr, 0, arr.length - 1, (arr.length - 1) \/ 2);\n\n    \/\/ \u8f93\u51fa\u4e2d\u95f4\u503c\n    System.out.println(\"\u4e2d\u95f4\u503c\u4e3a\uff1a\" + midValue);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u4e2d\u95f4\u503c\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 \u4e2d\u6c42\u6570\u7ec4\u4e2d\u95f4\u503c\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a1. \u4f7f\u7528\u6392\u5e8f\u540e\u53d6\u4e2d\uff0c\u5bf9\u6570\u7ec4\u6392\u5e8f\u540e\u53d6\u4e2d\u95f4\u7d22\u5f15\u5143\u7d20\uff1b2. \u4f7f\u7528\u5feb\u901f\u9009\u62e9\uff0c\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u90e8\u5206\uff0c\u627e\u5230\u6bd4\u57fa\u51c6\u5143\u7d20\u5927\u4e14\u5728\u524d\u534a\u90e8\u5206\uff08\u5947\u6570\u957f\u5ea6\uff09\u6216\u540e\u534a\u90e8\u5206\uff08\u5076\u6570\u957f\u5ea6\uff09\u7684\u5143\u7d20\u3002 Java \u6c42\u6570\u7ec4\u4e2d\u95f4\u503c\u7684\u4e24\u79cd\u65b9\u6cd5 \u5f00\u95e8\u89c1\u5c71\uff1a Java \u4e2d\u6c42\u6570\u7ec4\u4e2d\u95f4\u503c\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a \u8be6\u7ec6\u5c55\u5f00\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u65b9\u6cd5 1\uff1a\u4f7f\u7528\u6392\u5e8f\u540e\u53d6\u4e2d \u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a\u5947\u6570\uff0c\u4e2d\u95f4\u503c\u4e3a\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a (n-1)\/2 \u7684\u5143\u7d20\u3002 \u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a\u5076\u6570\uff0c\u4e2d\u95f4\u503c\u4e3a\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a n\/2 \u548c n\/2-1 \u7684\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002 \/\/ \u6570\u7ec4 arr int[] arr = {1, 3, 5, 2, 4}; \/\/ \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f Arrays.sort(arr); \/\/ \u6570\u7ec4\u957f\u5ea6\u4e3a\u5947\u6570 if (arr.length % 2 != 0) { int midIndex = (arr.length &#8211; 1) \/ 2; int midValue [&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-65924","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65924","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=65924"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65924\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}