{"id":656,"date":"2019-10-23T19:10:51","date_gmt":"2019-10-23T11:10:51","guid":{"rendered":"http:\/\/toradora.xyz\/?p=656"},"modified":"2019-10-23T19:10:51","modified_gmt":"2019-10-23T11:10:51","slug":"%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8fquicksort","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=656","title":{"rendered":"\u5feb\u901f\u6392\u5e8fQuickSort"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u901a\u8fc7\u9012\u5f52\u7b97\u6cd5\u5bf9\u6bd4\u57fa\u51c6\u503c\u800c\u8fdb\u884c\u5feb\u901f\u7684\u6392\u5e8f<\/p><\/blockquote>\n\n\n\n<p> \u5feb\u901f\u6392\u5e8f\u5229\u7528\u5206\u800c\u6cbb\u4e4b\u7684\u601d\u60f3\uff0c\u5b83\u7684\u6700\u597d\u548c\u5e73\u5747\u5b9e\u9645\u590d\u6742\u5ea6\u4e3aO(nlogn)\uff0c\u4f46\u662f\uff0c\u5982\u679c\u9009\u53d6\u57fa\u51c6\u7684\u89c4\u5219\u6b63\u597d\u4e0e\u5b9e\u9645\u6570\u503c\u5206\u5e03\u76f8\u53cd\uff0c\u4f8b\u5982\u6211\u4eec\u9009\u53d6\u7b2c\u4e00\u4e2a\u6570\u4e3a\u57fa\u51c6\uff0c\u800c\u539f\u59cb\u5e8f\u5217\u662f\u5012\u5e8f\u7684\uff0c\u90a3\u4e48\u6bcf\u4e00\u8f6e\u5faa\u73af\uff0c\u5feb\u6392\u90fd\u53ea\u80fd\u628a\u57fa\u51c6\u653e\u5230\u6700\u53f3\u4fa7\uff0c\u6545\u5feb\u6392\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n<sup>2<\/sup>)\u3002\u5feb\u6392\u7b97\u6cd5\u672c\u8eab\u6ca1\u6709\u7528\u5230\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u53ef\u4ee5\u8bf4\u9700\u8981\u7684\u7a7a\u95f4\u4e3aO(1)\uff1b\u5bf9\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u9700\u8981\u7684\u7a7a\u95f4\u662fO(n)\uff0c\u56e0\u4e3a\u5728\u9012\u5f52\u8c03\u7528\u65f6\u6709\u6808\u7684\u5f00\u9500\uff0c\u5f53\u7136\u6700\u574f\u60c5\u51b5\u662fO(n)\uff0c\u5e73\u5747\u60c5\u51b5\u662fO(logn)\u3002\u5feb\u901f\u6392\u5e8f\u662f\u4e0d\u7a33\u5b9a\u7684\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5b9e\u73b0\u4e00<\/p><cite>\u4ee5\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u6570\u4e3a\u57fa\u51c6<\/cite><\/blockquote>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class QuickSort {\n    public static void main(String[] args) {\n\t\tint[] arr={54,12,42,21,67,34,21};\n        quickSort(arr,0,arr.length-1);\n    }\n    \/\/\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u53d6\u627e\u57fa\u51c6\u6570\u7684\n    private static void quickSort(int[] arr, int left, int right) {\n         \/\/ \u9012\u5f52\u9700\u8981\u51fa\u53e3\n        if(left &gt; right){\n                return;\n        }\n        int i = left;\n        int j = right;\n        int baseNum = arr[left];\n        while( i != j){\n             while(arr[j] &gt;= baseNum &amp;&amp; j &gt; i ){\n                j--;\n             }\n             while(arr[i] &lt;= baseNum &amp;&amp; i &lt;j){\n                i++;\n             }\n             int temp = arr[i];\n             arr[i] = arr[j];\n             arr[j] = temp;\n        }\n        \/\/ \u76f8\u9047\u7684\u57fa\u51c6\u6570\u5f52\u4f4d\n        arr[left] = arr[j];\n        arr[j] = baseNum;\n\n        quickSort(arr,left,i-1);\n        quickSort(arr,j+1,right );\n\n    }\n}\n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5b9e\u73b0\u4e8c<\/p><cite>\u53d6\u4e2d\u95f4\u4e00\u4e2a\u6570\u4e3a\u57fa\u51c6\u5206\u754c\u503c<\/cite><\/blockquote>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class quickSort {\n    public static void main(String[] args) {\n        int[] arr={54,12,42,21,67,34,21};\n        System.out.println(&quot;\u6392\u5e8f\u524d\uff1a&quot;+ Arrays.toString(arr));\n        quickSort(arr,0,arr.length-1);\n        System.out.println(&quot;\u6392\u5e8f\u540e\uff1a&quot;+ Arrays.toString(arr));\n    }\n\n    public static void quickSort(int[] arr,int left,int right){\n        int ltemp=left,rtemp=right;\n        int temp,mid;\n        mid=arr[(left+right)\/2];                         \/\/\u5206\u754c\u503c\n        while(ltemp&lt;rtemp){\n            while (arr[ltemp]&lt;mid){\n                ltemp++;\n            }\n            while (arr[rtemp]&gt;mid){\n                rtemp--;\n            }\n            if(ltemp&lt;=rtemp){\n                temp=arr[ltemp];\n                arr[ltemp]=arr[rtemp];\n                arr[rtemp]=temp;\n                --rtemp;\n                ++ltemp;\n            }\n        }\n        if(ltemp==rtemp){\n            ltemp++;\n        }\n        if(left&lt;rtemp){\n            quickSort(arr,left,ltemp-1);               \/\/\u9012\u5f52\u8c03\u7528\n        }\n        if(ltemp&lt;right){\n            quickSort(arr,rtemp+1,right);               \/\/\u9012\u5f52\u8c03\u7528\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u9012\u5f52\u7b97\u6cd5\u5bf9\u6bd4\u57fa\u51c6\u503c\u800c\u8fdb\u884c\u5feb\u901f\u7684\u6392\u5e8f \u5feb\u901f\u6392\u5e8f\u5229\u7528\u5206\u800c\u6cbb\u4e4b\u7684\u601d\u60f3\uff0c\u5b83\u7684\u6700\u597d\u548c\u5e73\u5747\u5b9e\u9645\u590d\u6742\u5ea6\u4e3aO(nlogn)<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=656\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u5feb\u901f\u6392\u5e8fQuickSort<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/656"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=656"}],"version-history":[{"count":0,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}