{"id":3628,"date":"2023-01-19T16:33:37","date_gmt":"2023-01-19T08:33:37","guid":{"rendered":"https:\/\/qaqaq.top\/?p=3628"},"modified":"2023-01-19T16:33:38","modified_gmt":"2023-01-19T08:33:38","slug":"%e9%9d%9e%e9%80%92%e5%bd%92%e5%ae%9e%e7%8e%b0%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=3628","title":{"rendered":"\u975e\u9012\u5f52\u5b9e\u73b0\u5feb\u901f\u6392\u5e8f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package chapter4.part3;\r\n\r\n\/**\r\n * Created by weimengshu on 2018\/7\/13.\r\n *\/\r\nimport java.util.Arrays;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\nimport java.util.Stack;\r\n\r\npublic class QuickSortWithStack {\r\n\r\n    public static void quickSort(int&#91;] arr, int startIndex, int endIndex) {\r\n        \/\/ \u7528\u4e00\u4e2a\u96c6\u5408\u6808\u6765\u4ee3\u66ff\u9012\u5f52\u7684\u51fd\u6570\u6808\r\n        Stack&lt;Map&lt;String, Integer>> quickSortStack = new Stack&lt;Map&lt;String, Integer>>();\r\n        \/\/ \u6574\u4e2a\u6570\u5217\u7684\u8d77\u6b62\u4e0b\u6807\uff0c\u4ee5\u54c8\u5e0c\u7684\u5f62\u5f0f\u5165\u6808\r\n        Map rootParam = new HashMap();\r\n        rootParam.put(\"startIndex\", startIndex);\r\n        rootParam.put(\"endIndex\", endIndex);\r\n        quickSortStack.push(rootParam);\r\n\r\n        \/\/ \u5faa\u73af\u7ed3\u675f\u6761\u4ef6\uff1a\u6808\u4e3a\u7a7a\u65f6\u7ed3\u675f\r\n        while (!quickSortStack.isEmpty()) {\r\n            \/\/ \u6808\u9876\u5143\u7d20\u51fa\u6808\uff0c\u5f97\u5230\u8d77\u6b62\u4e0b\u6807\r\n            Map&lt;String, Integer> param = quickSortStack.pop();\r\n            \/\/ \u5f97\u5230\u57fa\u51c6\u5143\u7d20\u4f4d\u7f6e\r\n            int pivotIndex = partition(arr, param.get(\"startIndex\"), param.get(\"endIndex\"));\r\n            \/\/ \u6839\u636e\u57fa\u51c6\u5143\u7d20\u5206\u6210\u4e24\u90e8\u5206, \u628a\u6bcf\u4e00\u90e8\u5206\u7684\u8d77\u6b62\u4e0b\u6807\u5165\u6808\r\n            if(param.get(\"startIndex\") &lt;  pivotIndex -1){\r\n                Map&lt;String, Integer> leftParam = new HashMap&lt;String, Integer>();\r\n                leftParam.put(\"startIndex\",  param.get(\"startIndex\"));\r\n                leftParam.put(\"endIndex\", pivotIndex -1);\r\n                quickSortStack.push(leftParam);\r\n            }\r\n            if(pivotIndex + 1 &lt; param.get(\"endIndex\")){\r\n                Map&lt;String, Integer> rightParam = new HashMap&lt;String, Integer>();\r\n                rightParam.put(\"startIndex\", pivotIndex + 1);\r\n                rightParam.put(\"endIndex\", param.get(\"endIndex\"));\r\n                quickSortStack.push(rightParam);\r\n            }\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u5206\u6cbb\uff08\u5355\u8fb9\u5faa\u73af\u6cd5\uff09\r\n     * @param arr     \u5f85\u4ea4\u6362\u7684\u6570\u7ec4\r\n     * @param startIndex    \u8d77\u59cb\u4e0b\u6807\r\n     * @param endIndex    \u7ed3\u675f\u4e0b\u6807\r\n     *\/\r\n    private static int partition(int&#91;] arr, int startIndex, int endIndex) {\r\n        \/\/ \u53d6\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u7684\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u5143\u7d20\uff08\u4e5f\u53ef\u4ee5\u9009\u62e9\u968f\u673a\u4f4d\u7f6e\uff09\r\n        int pivot = arr&#91;startIndex];\r\n        int mark = startIndex;\r\n\r\n        for(int i=startIndex+1; i&lt;=endIndex; i++){\r\n            if(arr&#91;i]&lt;pivot){\r\n                mark ++;\r\n                int p = arr&#91;mark];\r\n                arr&#91;mark] = arr&#91;i];\r\n                arr&#91;i] = p;\r\n            }\r\n        }\r\n\r\n        arr&#91;startIndex] = arr&#91;mark];\r\n        arr&#91;mark] = pivot;\r\n        return mark;\r\n    }\r\n\r\n    public static void main(String&#91;] args) {\r\n        int&#91;] arr = new int&#91;] {4,7,6,5,3,2,8,1};\r\n        quickSort(arr, 0, arr.length-1);\r\n        System.out.println(Arrays.toString(arr));\r\n    }\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-3628","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\/3628"}],"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=3628"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3628\/revisions"}],"predecessor-version":[{"id":3629,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3628\/revisions\/3629"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}