{"id":2277,"date":"2019-11-07T14:03:44","date_gmt":"2019-11-07T06:03:44","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2277"},"modified":"2019-11-07T14:03:44","modified_gmt":"2019-11-07T06:03:44","slug":"%e3%80%90%e7%ae%97%e6%b3%95%e5%88%86%e6%9e%90%e4%b8%8e%e8%ae%be%e8%ae%a1%e3%80%91%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/introduction_to_algorithms\/2277\/","title":{"rendered":"\u3010\u7b97\u6cd5\u5bfc\u8bba\u3011\u5feb\u901f\u6392\u5e8f"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"253\" height=\"124\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-103.png\" alt=\"\" class=\"wp-image-2286\"\/><\/figure><\/div>\n\n\n<p>\u5bf9\u4e8e\u5305\u542bn\u4e2a\u6570\u7684\u8f93\u5165\u6570\u7ec4\u6765\u8bf4\uff0c\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u79cd\u6700\u5feb\u60c5\u51b5\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u867d\u7136\u6700\u574f\u60c5\u51b5\u65f6\u95f4\u590d\u6742\u5ea6\u5f88\u5dee\uff0c\u4f46\u662f\u5feb\u901f\u6392\u5e8f\u901a\u5e38\u662f\u5b9e\u9645\u6392\u5e8f\u5e94\u7528\u4e2d\u6700\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u7684\u5e73\u5747\u6027\u80fd\u975e\u5e38\u597d\uff0c\u671f\u671b\u662fO(nlgn)\uff0c\u800c\u4e14 O(nlgn) \u4e2d\u9690\u542b\u7684\u5e38\u6570\u56e0\u5b50\u975e\u5e38\u5c0f\uff0c\u5e76\u4e14\u5177\u6709\u539f\u5740\u6027\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">1.\u5feb\u901f\u6392\u5e8f\u7684\u63cf\u8ff0<\/h2>\n\n\n<p>\u5feb\u901f\u6392\u5e8f\u53ef\u4ee5\u7406\u89e3\u4e3a\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\uff0c\u4ee5\u8fd9\u4e2a\u57fa\u51c6\u5c06\u5217\u8868\u5206\u4e3a\u5927\u4e8e\u57fa\u51c6\u548c\u5c0f\u4e8e\u57fa\u51c6\u7684\u4e24\u90e8\u5206\uff0c\u5e76\u7c7b\u4f3c\u4e8c\u5206\u6cd5\u7ee7\u7eed\u5411\u4e0b\u9009\u62e9\u57fa\u51c6\u3002<\/p>\n\n\n<p>\u6bcf\u4e00\u8d9f\u5feb\u901f\u6392\u5e8f\u4e4b\u540e\u90fd\u6709\u4e00\u4e2a\u5143\u7d20\u4f4d\u4e8e\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n<p>\u7b80\u5355\u7684\u63cf\u8ff0\u5c31\u662f\uff1a<strong>\u4ece\u524d\u5411\u540e\u627e\u6bd4\u57fa\u51c6\u5927\u7684\u5e76\u4ea4\u6362\uff0c\u4ece\u540e\u5411\u524d\u627e\u6bd4\u57fa\u51c6\u5c0f\u7684\u5e76\u4ea4\u6362\u3002<\/strong><\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"296\" height=\"57\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-99.png\" alt=\"\" class=\"wp-image-2281\"\/><figcaption>\u4ee54\u4e3a\u57fa\u51c6\u7684\u4e00\u8d9f\u6392\u5e8f<\/figcaption><\/figure><\/div>\n\n\n<p>\u4ee3\u7801\uff0c\u65f6\u95f4\u590d\u6742\u5ea6O(n)\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def quick_sort(self, init_list, i):\n    <em>\"\"\"\n    \u5feb\u901f\u6392\u5e8f\n    <\/em><strong><em>:param<\/em><\/strong><em> init_list: \u6392\u5e8f\u5217\u8868\n    <\/em><strong><em>:param<\/em><\/strong><em> i: \u6bd4\u8f83\u5143\u7d20\u7d22\u5f15\n    <\/em><strong><em>:return<\/em><\/strong><em>:\n    \"\"\"\n    <\/em>length = len(init_list)\n    # \u8ba1\u7b97\u5de6\u53f3\u7d22\u5f15\n    left_index = 0\n    right_index = length - 1\n    # \u5c06\u6bd4\u8f83\u5143\u7d20\u653e\u5230\u6700\u540e\uff0c\u9632\u6b62\u5b83\u662f\u6700\u5927\u503c\u65f6\u4e0d\u8fdb\u884c\u4ea4\u6362\n    temp = init_list[i]\n    init_list[i] = init_list[right_index]\n    init_list[right_index] = temp\n    # \u6bd4\u8f83\u5143\u7d20\u503c\n    flag = init_list[right_index]\n    # \u6bd4\u8f83\u5143\u7d20\u7d22\u5f15\uff08\u56e0\u4e3a\u4ea4\u6362\u5143\u7d20\u4f4d\u7f6e\u4f1a\u53d8\uff09\n    flag_index = right_index\n    # \u91cd\u590d\u6267\u884c\u76f4\u81f3\u5de6\u53f3\u6807\u8bb0\u91cd\u53e0\n    while left_index != right_index:\n        # \u4ece\u5de6\u5411\u53f3\u627e\u6bd4flag\u5927\u7684\u6570\u4ea4\u6362\n        for i in range(left_index, right_index):\n            if flag &lt; init_list[left_index]:\n                temp = flag\n                init_list[flag_index] = init_list[left_index]\n                flag_index = i\n                init_list[left_index] = temp\n                break\n            else:\n                left_index += 1\n        # \u4ece\u53f3\u5411\u5de6\u627e\u6bd4flag\u5c0f\u7684\u6570\u4ea4\u6362\n        for i in range(right_index, left_index, -1):\n            if flag &gt; init_list[right_index]:\n                temp = flag\n                init_list[flag_index] = init_list[right_index]\n                flag_index = i\n                init_list[right_index] = temp\n                break\n            else:\n                right_index -= 1\n    return init_list<\/pre>\n\n\n<h2 class=\"wp-block-heading\">2.\u5feb\u901f\u6392\u5e8f\u7684\u6027\u80fd<\/h2>\n\n\n<p><strong>\u6700\u574f\u60c5\u51b5\u5212\u5206<\/strong><\/p>\n\n\n<p><\/p>\n\n\n<p>\u8003\u8651\u4e00\u79cd\u60c5\u51b5\uff1a\u6bcf\u6b21\u9009\u62e9\u7684\u57fa\u51c6\u90fd\u662f\u5f53\u524d\u5e8f\u5217\u4e2d\u6700\u5c0f\u7684\u5e8f\u5217\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884cn\u6b21\u6392\u5e8f\uff0c\u6bcf\u6b21\u6392\u5e8f\u4ee3\u4ef7\u662f O(n) \uff0c\u56e0\u6b64\u6700\u574f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f O(n^2)\u3002<\/p>\n\n\n<p><strong>\u6700\u597d\u60c5\u51b5\u5212\u5206<\/strong><\/p>\n\n\n<p>\u8003\u8651\u4e00\u79cd\u60c5\u51b5\uff1a\u6bcf\u6b21\u9009\u62e9\u7684\u57fa\u51c6\u90fd\u662f\u5f53\u524d\u5e8f\u5217\u4e2d\u4e2d\u95f4\u7684\u6570\u5b57\uff0c\u6bcf\u6b21\u9009\u62e9\u7684\u57fa\u51c6\u6b63\u597d\u5c06\u5e8f\u5217\u5206\u6210\u4e86\u4e00\u68f5\u6ee1\u4e8c\u53c9\u6811\uff0c\u56e0\u6b64\u6700\u597d\u60c5\u51b5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(nlgn)\u3002<\/p>\n\n\n<p><strong>\u5e73\u8861\u7684\u5212\u5206  <\/strong><\/p>\n\n\n<p>\u5373\u4f7f\u662f\u6ca1\u6709\u8fbe\u5230\u5b8c\u7f8e\u76841\/2\u5206\uff0c\u52a0\u5165\u662f9\uff1a1\u7684\u533a\u5206\uff0c\u4f9d\u7136\u53ef\u4ee5\u8fbe\u5230\u4e00\u4e2a O(nlgn)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"401\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-100.png\" alt=\"\" class=\"wp-image-2282\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-100.png 627w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-100-300x192.png 300w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"> 3.\u5feb\u901f\u6392\u5e8f\u7684\u968f\u673a\u5316\u7248\u672c<\/h2>\n\n\n<p>\u968f\u673a\u5316\u7684\u7248\u672c\u4e0d\u518d\u56fa\u5b9a\u7684\u9009\u62e9\u67d0\u4e00\u4e2a\u6570\uff0c\u800c\u662f\u968f\u673a\u9009\u62e9\u5411\u4e0b\u9012\u5f52\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7a0d\u5fae\u4fee\u6539\u4e00\u4e0b\u4e4b\u524d\u7684\u5feb\u6392\u4ee3\u7801\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5de6\u53f3\u6307\u9488\uff0c\u6307\u5b9a\u672c\u5730\u9012\u5f52\u9700\u8981\u6392\u5e8f\u7684\u8303\u56f4\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def quick_sort_for_random(self, init_list, i, left, right):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u5feb\u901f\u6392\u5e8f<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> init_list: \u6392\u5e8f\u5217\u8868<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> i: \u6bd4\u8f83\u5143\u7d20\u7d22\u5f15<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>length = len(init_list)<br \/>    # \u8ba1\u7b97\u5de6\u53f3\u7d22\u5f15<br \/>    left_index = left<br \/>    right_index = right<br \/>    # \u5c06\u6bd4\u8f83\u5143\u7d20\u653e\u5230\u6700\u540e\uff0c\u9632\u6b62\u5b83\u662f\u6700\u5927\u503c\u65f6\u4e0d\u8fdb\u884c\u4ea4\u6362<br \/>    temp = init_list[i]<br \/>    init_list[i] = init_list[right_index]<br \/>    init_list[right_index] = temp<br \/>    # \u6bd4\u8f83\u5143\u7d20\u503c<br \/>    flag = init_list[right_index]<br \/>    # \u6bd4\u8f83\u5143\u7d20\u7d22\u5f15\uff08\u56e0\u4e3a\u4ea4\u6362\u5143\u7d20\u4f4d\u7f6e\u4f1a\u53d8\uff09<br \/>    flag_index = right_index<br \/>    # \u91cd\u590d\u6267\u884c\u76f4\u81f3\u5de6\u53f3\u6807\u8bb0\u91cd\u53e0<br \/>    while left_index != right_index:<br \/>        # \u4ece\u5de6\u5411\u53f3\u627e\u6bd4flag\u5927\u7684\u6570\u4ea4\u6362<br \/>        for i in range(left_index, right_index):<br \/>            if flag &lt; init_list[left_index]:<br \/>                temp = flag<br \/>                init_list[flag_index] = init_list[left_index]<br \/>                flag_index = i<br \/>                init_list[left_index] = temp<br \/>                break<br \/>            else:<br \/>                left_index += 1<br \/>        # \u4ece\u53f3\u5411\u5de6\u627e\u6bd4flag\u5c0f\u7684\u6570\u4ea4\u6362<br \/>        for i in range(right_index, left_index, -1):<br \/>            if flag &gt; init_list[right_index]:<br \/>                temp = flag<br \/>                init_list[flag_index] = init_list[right_index]<br \/>                flag_index = i<br \/>                init_list[right_index] = temp<br \/>                break<br \/>            else:<br \/>                right_index -= 1<br \/>    return init_list, left_index<\/pre>\n\n\n<p>\u7136\u540e\u662f\u968f\u673a\u5feb\u6392\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def random_quick_sort(self, init_list, left, right):<br \/><br \/>    <em>\"\"\"<br \/><\/em><em>    \u968f\u673a\u5feb\u901f\u6392\u5e8f<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> init_list:\u5217\u8868<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em># \u5982\u679c\u662f\u5355\u4e00\u5143\u7d20\u6392\u5e8f\uff0c\u76f4\u63a5\u8fd4\u56de<br \/>    if right == left:<br \/>        return init_list<br \/>    # \u968f\u673a\u521d\u59cb\u5316\u4e00\u4e2a\u76ee\u6807\u5143\u7d20<br \/>    random_index = random.randint(left, right)<br \/>    # \u8fdb\u884c\u6392\u5e8f\uff0c\u83b7\u53d6\u5230\u6392\u5e8f\u540e\u7684\u5217\u8868\u4ee5\u53ca\u76ee\u6807\u5143\u7d20\u73b0\u5728\u7684\u7d22\u5f15<br \/>    sort_list, sort_flag_index = self.quick_sort_for_random(init_list.copy(), random_index, left, right)<br \/>    # \u5982\u679c\u5f53\u524d\u76ee\u6807\u4e0d\u662f\u6700\u5c0f\u503c\uff0c\u5219\u6392\u5e8f\u5de6\u5b50\u6811<br \/>    if sort_flag_index != left:<br \/>        sort_list = self.random_quick_sort(sort_list, left, sort_flag_index - 1)<br \/>    # \u5982\u679c\u5f53\u524d\u76ee\u6807\u4e0d\u662f\u6700\u5927\u503c\uff0c\u6392\u5e8f\u53f3\u5b50\u6811<br \/>    if sort_flag_index != right:<br \/>        sort_list = self.random_quick_sort(sort_list, sort_flag_index + 1, right)<br \/>    return sort_list<\/pre>\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"53\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-101.png\" alt=\"\" class=\"wp-image-2284\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-101.png 314w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-101-300x51.png 300w\" sizes=\"(max-width: 314px) 100vw, 314px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u5305\u542bn\u4e2a\u6570\u7684\u8f93\u5165\u6570\u7ec4\u6765\u8bf4\uff0c\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u79cd\u6700\u5feb\u60c5\u51b5\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u867d\u7136\u6700\u574f\u60c5\u51b5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[11],"tags":[],"views":3389,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2277"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2277"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2277\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2277"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}