{"id":201,"date":"2013-06-17T20:18:34","date_gmt":"2013-06-17T12:18:34","guid":{"rendered":"http:\/\/coderbee.net\/?p=201"},"modified":"2014-11-28T20:32:34","modified_gmt":"2014-11-28T12:32:34","slug":"%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/algorithm\/20130617\/201","title":{"rendered":"\u5feb\u901f\u6392\u5e8f"},"content":{"rendered":"<p>\u5feb\u901f\u6392\u5e8f\u4e5f\u662f\u57fa\u4e8e\u5206\u6cbb\u6a21\u5f0f\u7684\uff0c\u5bf9\u4e8e\u89c4\u6a21\u4e3an\u7684\u6570\u7ec4\uff0c\u6700\u574f\u60c5\u51b5\u8fd0\u884c\u65f6\u95f4\u662fO(n^2)\uff0c\u5e73\u5747\u8fd0\u884c\u65f6\u95f4\u662fO(lg(n))\u3002<\/p>\n<p>\u5bf9\u6570\u7ec4A[p&#8230;r]\u6392\u5e8f\u7684\u5206\u6cbb\u8fc7\u7a0b\u7684\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u5206\u89e3\uff1a\u6570\u7ec4A[p&#8230;r]\u88ab\u5212\u5206\u6210\u4e24\u4e2a\uff08\u53ef\u80fd\u7a7a\uff09\u5b50\u6570\u7ec4A[p&#8230;q-1]\u548cA[q+1&#8230;r]\uff0c\u4f7f\u5f97A[p&#8230;q-1]\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u5c0f\u4e8e\u7b49\u4e8eA[q]\uff0c\u800c\u4e14\u5c0f\u4e8e\u7b49\u4e8eA[q+1&#8230;r]\u4e2d\u7684\u5143\u7d20\u3002\u4e0b\u6807q\u4e5f\u5728\u8fd9\u4e2a\u5212\u5206\u8fc7\u7a0b\u4e2d\u8ba1\u7b97\u3002   <\/li>\n<li>\u89e3\u51b3\uff1a\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u5feb\u901f\u6392\u5e8f\uff0c\u5bf9\u5b50\u6570\u7ec4A[p&#8230;q-1]\u548cA[q+1&#8230;r]\u6392\u5e8f\u3002   <\/li>\n<li>\u5408\u5e76\uff1a\u56e0\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u662f\u5c31\u5730\u6392\u5e8f\uff0c\u5c06\u4ed6\u4eec\u5408\u5e76\u4e0d\u9700\u8981\u64cd\u4f5c\uff1a\u6574\u4e2a\u6570\u7ec4A[p&#8230;r]\u5df2\u7ecf\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u4ece\u4e0a\u9762\u7684\u6b65\u9aa4\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u5feb\u901f\u6392\u5e8f\u7684\u5173\u952e\u5728\u5212\u5206\u5b50\u6570\u7ec4\uff0c\u4e14\u6bcf\u6b21\u5212\u5206\u90fd\u4ea7\u751f\u4e00\u4e2a\u5143\u7d20\uff0c\u5b83\u6240\u5728\u7684\u4f4d\u7f6e\u5c31\u662f\u5b83\u6700\u7ec8\u7684\u4f4d\u7f6e\u3002<\/strong><br \/>\n<!--more--><\/p>\n<h2>\u5b9e\u73b0<\/h2>\n<pre><code language=\"go\">\nfunc quickSortRecur(arr []int, low, high int) {\n    if low < high {\n        \/\/p := hoarePartition(arr, low, high)\n        p := partition(arr, low, high)\n        quickSortRecur(arr, low, p-1)\n        quickSortRecur(arr, p+1, high)\n    }\n}\n\nfunc partition(arr []int, low, high int) int {\n    v := arr[high]\n    l, h := low-1, low\n    for ; h < high; h++ {\n        if arr[h] < v {\n            l = l + 1\n            util.Swap(arr, l, h)\n        }\n    }\n    l = l + 1\n    util.Swap(arr, l, high)\n    return l\n}\n\nfunc hoarePartition(arr []int, low, high int) int {\n    l, r := low+1, high\n    v := arr[low]\n    for {\n        for ; l < r &#038;&#038; arr[l] < v; l++ {\n        }\n\n        for ; r >= l && arr[r] >= v; r-- {\n        }\n\n        if l < r {\n            util.Swap(arr, l, r)\n            l, r = l+1, r-1\n        } else {\n            util.Swap(arr, r, low)\n            return r\n        }\n    }\n}\n<\/code><\/pre>\n<h2>\u8bf4\u660e<\/h2>\n<h3>partition\u51fd\u6570<\/h3>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684partition\u51fd\u6570\u7684\u5212\u5206\u8fc7\u7a0b\uff0c\u662f\u628a\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u90e8\u5206arr[low...high]\u5148\u5206\u62104\u4e2a\u90e8\u5206\uff0c\u4ee5\u4ece\u5de6\u5f80\u53f3\u7684\u65b9\u5411\u770b\uff0cl\u662f\u6307\u5411\u5c0f\u4e8epivot value\u503c\u7684\u5b50\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\uff0ch\u662f\u6307\u5411\u5927\u4e8e\u7b49\u4e8epivot value\u503c\u7684\u5b50\u6570\u7ec4\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\uff1a<\/p>\n<ul>\n<li>pivot value\uff0c\u521d\u59cb\u503c\u4e3aarr[high]\uff0c\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u653e\u5728\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u4e0a\u3002<\/li>\n<li>\u5c0f\u4e8epivot value\u7684\u5b50\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a\u7a7a\uff0c\u8303\u56f4\u4e3aarr[0, l]\u3002<\/li>\n<li>\u5927\u4e8epivot value\u7684\u5b50\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a\u7a7a\uff0c\u8303\u56f4\u4e3aarr[l, h]\u3002<\/li>\n<li>\u672a\u6392\u5e8f\u7684\u5b50\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3aarr[low, high-1]\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u56fe\u5c55\u793a\u4e86\u5212\u5206\u8fc7\u7a0b\uff0c\u5b83\u7684\u6307\u9488\u4e0e\u51fd\u6570\u53d8\u91cf\u7684\u5bf9\u5e94\u5173\u7cfb\uff1a <code>i - l, j - h,  p - low,  r - high<\/code><a href=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/06\/quick-sort-partition.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/06\/quick-sort-partition-144x300.png\" alt=\"quick-sort-partition\" width=\"144\" height=\"300\" class=\"alignnone size-medium wp-image-202\" srcset=\"https:\/\/coderbee.net\/wp-content\/uploads\/2013\/06\/quick-sort-partition-144x300.png 144w, https:\/\/coderbee.net\/wp-content\/uploads\/2013\/06\/quick-sort-partition.png 172w\" sizes=\"(max-width: 144px) 100vw, 144px\" \/><\/a><\/p>\n<h3>hoarePartition\u51fd\u6570<\/h3>\n<p>\u4e0e\u524d\u9762\u7684partition\u51fd\u6570\u7c7b\u4f3c\uff0c\u4e5f\u662f\u628a\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u5212\u5206\u4e3a4\u4e2a\u5b50\u6570\u7ec4\uff0c\u53ea\u4e0d\u8fc7l\u3001h\u6307\u9488\u662f\u76f8\u5411\u800c\u884c\uff0c\u800cpartition\u51fd\u6570\u7684\u6307\u9488l\u3001h\u6307\u9488\u662f\u901a\u5411\u524d\u884c\u3002<\/p>\n<h2>\u5173\u4e8e\u5206\u533a\u51fd\u6570<\/h2>\n<p>\u524d\u9762\u7684\u5206\u533a\u51fd\u6570\u90fd\u662f\u9009\u62e9\u6700\u4f4e\u4f4d\u6216\u6700\u9ad8\u4f4d\u5143\u7d20\u4f5c\u4e3a\u54e8\u5175\u5143\u7d20\uff0c\u8fd9\u5728\u6570\u7ec4\u6709\u5e8f\u6216\u9006\u5e8f\u7684\u6781\u7aef\u60c5\u51b5\u4e0b\u662f\u5f88\u7cdf\u7cd5\u7684\uff0c\u53ef\u80fd\u5bfc\u81f4\u5212\u5206\u7684\u5143\u7d20\u603b\u662f\u5904\u4e8e\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\uff0c\u800c\u4e0d\u80fd\u5c3d\u53ef\u80fd\u5e73\u5747\u7684\u5212\u5206\u3002<\/p>\n<p>\u4e00\u4e9b\u6539\u8fdb\u7684\u65b9\u6cd5\u662f\u53d6\u4e2d\u95f4\u7684\u5143\u7d20\u6216\u968f\u673a\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u54e8\u5175\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u6392\u5e8f\u4e5f\u662f\u57fa\u4e8e\u5206\u6cbb\u6a21\u5f0f\u7684\uff0c\u5bf9\u4e8e\u89c4\u6a21\u4e3an\u7684\u6570\u7ec4\uff0c\u6700\u574f\u60c5\u51b5\u8fd0\u884c\u65f6\u95f4\u662fO(n^2)\uff0c &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/algorithm\/20130617\/201\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[55,56],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/201"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":1110,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions\/1110"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}