{"id":3630,"date":"2023-01-19T16:34:00","date_gmt":"2023-01-19T08:34:00","guid":{"rendered":"https:\/\/qaqaq.top\/?p=3630"},"modified":"2023-01-19T16:34:01","modified_gmt":"2023-01-19T08:34:01","slug":"%e6%a1%b6%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=3630","title":{"rendered":"\u6876\u6392\u5e8f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package chapter4.part4;\r\n\r\n\/**\r\n * Created by weimengshu on 2018\/7\/13.\r\n *\/\r\nimport java.util.Arrays;\r\n\r\npublic class HeapSort {\r\n\r\n    \/**\r\n     * \u4e0b\u6c89\u8c03\u6574\r\n     * @param array     \u5f85\u8c03\u6574\u7684\u5806\r\n     * @param parentIndex    \u8981\u4e0b\u6c89\u7684\u7236\u8282\u70b9\r\n     * @param length    \u5806\u7684\u6709\u6548\u5927\u5c0f\r\n     *\/\r\n    public static void downAdjust(int&#91;] array, int parentIndex, int length) {\r\n        \/\/ temp\u4fdd\u5b58\u7236\u8282\u70b9\u503c\uff0c\u7528\u4e8e\u6700\u540e\u7684\u8d4b\u503c\r\n        int temp = array&#91;parentIndex];\r\n        int childIndex = 2 * parentIndex + 1;\r\n        while (childIndex &lt; length) {\r\n            \/\/ \u5982\u679c\u6709\u53f3\u5b69\u5b50\uff0c\u4e14\u53f3\u5b69\u5b50\u5927\u4e8e\u5de6\u5b69\u5b50\u7684\u503c\uff0c\u5219\u5b9a\u4f4d\u5230\u53f3\u5b69\u5b50\r\n            if (childIndex + 1 &lt; length &amp;&amp; array&#91;childIndex + 1] > array&#91;childIndex]) {\r\n                childIndex++;\r\n            }\r\n            \/\/ \u5982\u679c\u7236\u8282\u70b9\u5927\u4e8e\u7b49\u4e8e\u4efb\u4f55\u4e00\u4e2a\u5b69\u5b50\u7684\u503c\uff0c\u76f4\u63a5\u8df3\u51fa\r\n            if (temp >= array&#91;childIndex])\r\n                break;\r\n            \/\/\u65e0\u9700\u771f\u6b63\u4ea4\u6362\uff0c\u5355\u5411\u8d4b\u503c\u5373\u53ef\r\n            array&#91;parentIndex] = array&#91;childIndex];\r\n            parentIndex = childIndex;\r\n            childIndex = 2 * childIndex + 1;\r\n        }\r\n        array&#91;parentIndex] = temp;\r\n    }\r\n\r\n    \/**\r\n     * \u5806\u6392\u5e8f(\u5347\u5e8f)\r\n     * @param array     \u5f85\u8c03\u6574\u7684\u5806\r\n     *\/\r\n    public static void heapSort(int&#91;] array) {\r\n        \/\/ 1.\u628a\u65e0\u5e8f\u6570\u7ec4\u6784\u5efa\u6210\u6700\u5927\u5806\u3002\r\n        for (int i = (array.length-2)\/2; i >= 0; i--) {\r\n            downAdjust(array, i, array.length);\r\n        }\r\n        System.out.println(Arrays.toString(array));\r\n        \/\/ 2.\u5faa\u73af\u4ea4\u6362\u96c6\u5408\u5c3e\u90e8\u5143\u7d20\u5230\u5806\u9876\uff0c\u5e76\u8c03\u8282\u5806\u4ea7\u751f\u65b0\u7684\u5806\u9876\u3002\r\n        for (int i = array.length - 1; i > 0; i--) {\r\n            \/\/ \u6700\u540e\u4e00\u4e2a\u5143\u7d20\u548c\u7b2c\u4e00\u5143\u7d20\u8fdb\u884c\u4ea4\u6362\r\n            int temp = array&#91;i];\r\n            array&#91;i] = array&#91;0];\r\n            array&#91;0] = temp;\r\n            \/\/ \u4e0b\u6c89\u8c03\u6574\u6700\u5927\u5806\r\n            downAdjust(array, 0, i);\r\n        }\r\n    }\r\n\r\n    public static void main(String&#91;] args) {\r\n        int&#91;] arr = new int&#91;] {1,3,2,6,5,7,8,9,10,0};\r\n        heapSort(arr);\r\n        System.out.println(Arrays.toString(arr));\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[133],"class_list":["post-3630","post","type-post","status-publish","format-standard","hentry","category-82","tag-133"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3630"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3630"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3630\/revisions"}],"predecessor-version":[{"id":3631,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3630\/revisions\/3631"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}