{"id":2442,"date":"2019-11-14T13:51:55","date_gmt":"2019-11-14T05:51:55","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2442"},"modified":"2019-11-14T13:51:55","modified_gmt":"2019-11-14T05:51:55","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%e4%b8%ad%e4%bd%8d%e6%95%b0%e5%92%8c%e9%a1%ba%e5%ba%8f%e7%bb%9f%e8%ae%a1%e9%87%8f","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/introduction_to_algorithms\/2442\/","title":{"rendered":"\u3010\u7b97\u6cd5\u5bfc\u8bba\u3011\u4e2d\u4f4d\u6570\u548c\u987a\u5e8f\u7edf\u8ba1\u91cf"},"content":{"rendered":"\n<p><strong>\u4e2d\u4f4d\u6570<\/strong>\uff1a\u5206\u4e3a\u4e0a\u4e2d\u4f4d\u6570\u548c\u4e0b\u4e2d\u4f4d\u6570\uff0c\u4e0a\u4e2d\u4f4d\u6570\u5373\u957f\u5ea6\u96642\u5411\u4e0a\u53d6\u6574\uff0c\u4e0b\u4e2d\u4f4d\u6570\u4e3a\u957f\u5ea6\u96642\u5411\u4e0b\u53d6\u6574\u3002\u4e00\u822c\u8bf4\u4e2d\u4f4d\u6570\u9ed8\u8ba4\u4e0b\u4e2d\u4f4d\u6570\u3002<\/p>\n\n\n<p><strong>\u987a\u5e8f\u7edf\u8ba1\u91cf<\/strong>\uff1a\u5373\u7b2c\u51e0\u5c0f\u7684\u6570\u3002\u4e00\u4e2a\u6570\u7ec4\u7684\u7b2c\u4e00\u987a\u5e8f\u7edf\u8ba1\u91cf\u5373\u4e3a\u6700\u5c0f\u7684\u6570\uff0c\u7b2cn\u987a\u5e8f\u7edf\u8ba1\u91cf\u5373\u4e3a\u6700\u5927\u7684\u6570\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">1.\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c<\/h2>\n\n\n<p>\u5982\u679c\u6211\u4eec\u8981\u627e\u51fa\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u6700\u5c0f\u503c\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884cn-1\u6b21\u6bd4\u8f83\uff0c\u540c\u6837\uff0c\u5982\u679c\u6211\u4eec\u8981\u627e\u51fa\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u503c\uff0c\u4e5f\u9700\u8981\u8fdb\u884cn-1\u6b21\u6bd4\u8f83\u3002<\/p>\n\n\n<p>\u90a3\u5982\u679c\u6211\u4eec\u9700\u8981\u540c\u65f6\u627e\u51fa\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u5462\uff0c\u90a3\u5c31\u9700\u89812n-2\u6b21\u6bd4\u8f83\u3002<\/p>\n\n\n<p>\u4f46\u662f\u5982\u679c\u6211\u4eec\u4e0d\u518d\u662f\u7528\u4e00\u4e2a\u6570\u4e0e\u5df2\u7ecf\u8bb0\u5f55\u7684\u6700\u5927\u6700\u5c0f\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u800c\u662f\u7528\u4e24\u4e2a\u6570\u5148\u6bd4\u8f83\uff0c\u7136\u540e\u53d6\u8f83\u5c0f\u7684\u4e00\u4e2a\u548c\u6700\u5927\u503c\u6bd4\u8f83\uff0c\u8f83\u5927\u7684\u548c\u6700\u5927\u503c\u6bd4\u8f83\uff0c\u8fd9\u6837\u4e09\u6b21\u6bd4\u8f83\u5c31\u53ef\u4ee5\u5b8c\u6210\u4e24\u4e2a\u6570\u7684\u6bd4\u8f83\uff0c\u8f83\u4e0a\u9762\u7684\u65b9\u6cd5\u8282\u7ea6\u4e86\u4e00\u6b21\u6bd4\u8f83\uff0c\u56e0\u800c\u6211\u4eec\u53ea\u9700\u8981<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"65\" height=\"26\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-114.png\" alt=\"\" class=\"wp-image-2443\"\/><\/figure><\/div>\n\n\n<p>\u6b21\u6bd4\u8f83\u5373\u53ef\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">2.\u671f\u671b\u4e3a\u7ebf\u6027\u65f6\u95f4\u7684\u9009\u62e9\u7b97\u6cd5<\/h2>\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bcf\u4e00\u8d9f\u7684\u7ed3\u679c\uff0c\u6807\u8bb0\u5143\u7d20\u90fd\u4f1a\u843d\u5728\u5b83\u6700\u7ec8\u7ed3\u679c\u7684\u4f4d\u7f6e\u4e0a\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u6027\u8d28\uff0c\u8fbe\u5230\u4ee5\u7ebf\u6027\u65f6\u95f4\u6765\u5bfb\u627e\u5e8f\u5217\u7684\u987a\u5e8f\u7edf\u8ba1\u91cf\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def quick_sort(self, init_list, i, left, right):\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 = left\n    right_index = right\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 > 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, left_index\ndef randomized_select(self, init_list, order_statistic_idx, left, right):\n    <em>\"\"\"\n    \u5229\u7528\u5feb\u901f\u6392\u5e8f\u627e\u51fa\u7b2cn\u4e2a\u987a\u5e8f\u7edf\u8ba1\u91cf\n    <\/em><strong><em>:param<\/em><\/strong><em> init_list:\n    <\/em><strong><em>:param<\/em><\/strong><em> order_statistic_idx:\n    <\/em><strong><em>:return<\/em><\/strong><em>:\n    \"\"\"\n    <\/em>if len(init_list) == 1:\n        return init_list\n    random_index = random.randint(left, right)\n    init_list, flag_idx = self.quick_sort(init_list, random_index, left, right)\n    # \u8fd9\u91cc\u8fdb\u884c\u4e86\u526a\u679d\uff0c\u4e0d\u9700\u8981\u60f3\u5feb\u6392\u4e00\u6837\u5de6\u53f3\u5747\u6392\u5e8f\uff0c\u9009\u62e9\u4e00\u4fa7\u7684\u8fdb\u884c\u6392\u5e8f\u5373\u53ef\n    if flag_idx == order_statistic_idx:\n        return init_list[flag_idx]\n    elif flag_idx > order_statistic_idx:\n        return self.randomized_select(init_list, order_statistic_idx, left, flag_idx - 1)\n    else:\n        return self.randomized_select(init_list, order_statistic_idx, flag_idx + 1, right)<\/pre>\n\n\n<h2 class=\"wp-block-heading\">3.\u6700\u574f\u60c5\u51b5\u4e3a\u7ebf\u6027\u65f6\u95f4\u7684\u9009\u62e9\u7b97\u6cd5<\/h2>\n\n\n<p>\u901a\u8fc7\u5c06\u5e8f\u5217\u5206\u533a\u6392\u5e8f\u67e5\u627e\u4e2d\u4f4d\u6570\u7684\u64cd\u4f5c\u6765\u786e\u5b9a\u6574\u4e2a\u6574\u4e2a\u5e8f\u5217\u7684\uff08\u8fd1\u4f3c\uff09\u4e2d\u4f4d\u6570\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e2a\u4e2d\u4f4d\u6570\u6267\u884c\u4e00\u8d9f\u5feb\u6392\uff0c\u7136\u540e\u5c31\u548c\u4e0a\u9762\u7c7b\u4f3c\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e2d\u4f4d\u6570\uff1a\u5206\u4e3a\u4e0a\u4e2d\u4f4d\u6570\u548c\u4e0b\u4e2d\u4f4d\u6570\uff0c\u4e0a\u4e2d\u4f4d\u6570\u5373\u957f\u5ea6\u96642\u5411\u4e0a\u53d6\u6574\uff0c\u4e0b\u4e2d\u4f4d\u6570\u4e3a\u957f\u5ea6\u96642\u5411\u4e0b\u53d6\u6574\u3002\u4e00\u822c\u8bf4\u4e2d\u4f4d\u6570 [&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":4752,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2442"}],"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=2442"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2442\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2442"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}