{"id":2160,"date":"2019-11-05T21:57:42","date_gmt":"2019-11-05T13:57:42","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2160"},"modified":"2019-11-05T21:57:42","modified_gmt":"2019-11-05T13:57:42","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%a0%86%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/introduction_to_algorithms\/2160\/","title":{"rendered":"\u3010\u7b97\u6cd5\u5bfc\u8bba\u3011\u5806\u6392\u5e8f"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"245\" height=\"183\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-97.png\" alt=\"\" class=\"wp-image-2273\"\/><\/figure><\/div>\n\n\n<p>\u5806\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(nlgn)\uff0c\u5177\u6709\u539f\u5740\u6027\u3002<\/p>\n\n\n<p>\u540c\u65f6\u9700\u8981\u533a\u5206\u201c\u5783\u573e\u6536\u96c6\u5b58\u50a8\u673a\u5236\u201d\u4e2d\u7684\u5806\uff0c\u4f8b\u5982Java\u8bed\u8a00\u4e2d\u7684\u5b9a\u4e49\uff0c\u8fd9\u91cc\u7684\u5806\u4e0d\u662f\u5783\u573e\u6536\u96c6\u5b58\u50a8\uff0c\u53ea\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">1.\u5806<\/h2>\n\n\n<p> \u5982\u4e0b\u56fe\u662f\u4e00\u4e2a\u4e8c\u53c9\u5806\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"659\" height=\"325\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-11.png\" alt=\"\" class=\"wp-image-2162\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-11.png 659w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-11-300x148.png 300w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure><\/div>\n\n\n<p>\u4e8c\u53c9\u5806\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u5f62\u5f0f\uff1a<strong>\u6700\u5927\u5806<\/strong>\u548c<strong>\u6700\u5c0f\u5806<\/strong>\u3002<\/p>\n\n\n<p>\u6700\u5927\u5806\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u9012\u51cf\uff08\u5e38\u7528\u4e8e\u6392\u5e8f\uff09\uff0c\u6700\u5c0f\u5806\u662f\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u9012\u589e\uff08\u5e38\u7528\u4e8e\u6784\u9020\u4f18\u5148\u961f\u5217\uff09\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">2 \u7ef4\u62a4\u5806\u7684\u6027\u8d28<\/h2>\n\n\n<p>\u5f53\u65b0\u7684\u6570\u636e\u88ab\u63d2\u5165\u65f6\uff0c\u7406\u6240\u5e94\u5f53\u9700\u8981\u7ef4\u62a4\u8fd9\u4e2a\u6811\uff0c\u8ba9\u4ed6\u4fdd\u6301\u5806\u7684\u6027\u8d28\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"416\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-92.png\" alt=\"\" class=\"wp-image-2267\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-92.png 760w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-92-300x164.png 300w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><\/figure><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"422\" height=\"54\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-93.png\" alt=\"\" class=\"wp-image-2268\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-93.png 422w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-93-300x38.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/figure><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"53\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-94.png\" alt=\"\" class=\"wp-image-2269\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-94.png 381w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-94-300x42.png 300w\" sizes=\"(max-width: 381px) 100vw, 381px\" \/><\/figure><\/div>\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(lgn)\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def max_heapify(self, heap_list, i):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u8c03\u6574\u6700\u5927\u5806<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> heap_list:\u9700\u8981\u88ab\u8c03\u6574\u7684\u961f\u5217<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> i: \u9700\u8981\u88ab\u8c03\u6574\u7684\u8282\u70b9<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>list_length = len(heap_list)<br \/>    # \u5206\u522b\u8ba1\u7b97\u5de6\u5b69\u5b50\u548c\u53f3\u5b69\u5b50\u7d22\u5f15<br \/>    left_point_index = 2 * i<br \/>    right_point_index = left_point_index + 1<br \/>    # \u5224\u65ad\u5de6\u5b69\u5b50\u662f\u5426\u6bd4\u6839\u8282\u70b9\u5927\uff0c\u5982\u679c\u5927\uff0c\u8bf4\u660e\u5de6\u5b50\u6811\u9700\u8981\u8c03\u6574<br \/>    if left_point_index &lt; list_length and heap_list[left_point_index] &gt; heap_list[i]:<br \/>        largest = left_point_index<br \/>    # \u5982\u679c\u5de6\u5b69\u5b50\u6bd4\u6839\u8282\u70b9\u5c0f\uff0c\u8bf4\u660e\u5de6\u5b50\u6811\u4e3a\u6700\u5927\u5806<br \/>    else:<br \/>        largest = i<br \/>    # \u5224\u65ad\u53f3\u5b50\u6811\u662f\u5426\u9700\u8981\u8c03\u6574<br \/>    if right_point_index &lt; list_length and heap_list[right_point_index] &gt; heap_list[largest]:<br \/>        largest = right_point_index<br \/>    # \u5982\u679c\u9700\u8981\u8c03\u6574\uff0c\u4ea4\u6362\u4e24\u4e2a\u7d22\u5f15\u4e2d\u7684\u6570\u5e76\u7ee7\u7eed\u5411\u4e0b\u8c03\u6574\u5b50\u6811<br \/>    if largest != i:<br \/>        temp = heap_list[largest]<br \/>        heap_list[largest] = heap_list[i]<br \/>        heap_list[i] = temp<br \/>        self.max_heapify(heap_list, largest)<br \/>    return heap_list<\/pre>\n\n\n<h2 class=\"wp-block-heading\">3.\u5efa\u5806<\/h2>\n\n\n<p>\u5efa\u5806\u53ef\u4ee5\u89c6\u4e3a\u8fdb\u884cn\u6b21\u8c03\u6574\u5806\u7684\u8fc7\u7a0b\uff0c\u540c\u65f6\uff0c\u53ef\u4ee5\u53ea\u904d\u5386\u524d\u534a,\u56e0\u4e3a\u6ee1\u4e8c\u53c9\u6811\u7684\u6027\u8d28\u51b3\u5b9a\u4e86\u540e\u4e00\u534a\u4e3a\u53f6\u5b50\u8282\u70b9\uff0c\u53ef\u4ee5\u89c6\u4e3a\u5df2\u7ecf\u6392\u597d\u5e8f\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"56\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-95.png\" alt=\"\" class=\"wp-image-2270\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-95.png 397w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-95-300x42.png 300w\" sizes=\"(max-width: 397px) 100vw, 397px\" \/><\/figure><\/div>\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def build_max_heap(self, init_list):\n    <em>\"\"\"\n    \u6784\u5efa\u6700\u5927\u5806\n    <\/em><strong><em>:param<\/em><\/strong><em> init_list: \u5217\u8868\n    <\/em><strong><em>:return<\/em><\/strong><em>:\n    \"\"\"\n    <\/em>max_heap = []\n    # \u8fd9\u91cc\u53ef\u4ee5\u904d\u5386\u5168\u90e8\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u53ea\u904d\u5386\u524d\u534a,\u56e0\u4e3a\u6ee1\u4e8c\u53c9\u6811\u7684\u6027\u8d28\u51b3\u5b9a\u4e86\u540e\u4e00\u534a\u4e3a\u53f6\u5b50\u8282\u70b9\uff0c\u53ef\u4ee5\u89c6\u4e3a\u5df2\u7ecf\u6392\u597d\u5e8f\n    for i in range(int(len(init_list) \/ 2), 0, -1):\n        max_heap = self.max_heapify(init_list, i)\n        print(max_heap)\n    return max_heap<\/pre>\n\n\n<h2 class=\"wp-block-heading\">4.\u5806\u6392\u5e8f\u7b97\u6cd5<\/h2>\n\n\n<p>\u6700\u5927\u5806\u7684\u6839\u6c38\u8fdc\u90fd\u662f\u5217\u8868\u4e2d\u6700\u5927\u7684\u6570\uff0c\u56e0\u6b64\u5982\u679c\u5728\u4e0d\u9700\u8981\u5168\u6392\u5217\u7684\u60c5\u51b5\u4e0b\uff0c\u5806\u6392\u5e8f\u662f\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"276\" src=\"\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-96.png\" alt=\"\" class=\"wp-image-2272\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-96.png 725w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/11\/\u56fe\u7247-96-300x114.png 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/figure><\/div>\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(nlgn):<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def heap_sort(self, init_list):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u5806\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>sort_list = []<br \/>    # \u8fdb\u884c\u7b2c\u4e00\u6b21\u5806\u6392\u5e8f<br \/>    max_heap = self.build_max_heap(init_list)<br \/>    # \u5c06\u7b2c\u4e00\u6b21\u6392\u5e8f\u7684\u9996\u5143\u7d20\u52a0\u5230\u6392\u5e8f\u5217\u8868\u4e2d\uff08\u4e0b\u6807\u4ece1\u5f00\u59cb\uff09<br \/>    # sort_list.append(max_heap[1])<br \/>    # \u53d6\u51fa\u7b2c\u4e00\u4e2a\u6570\u5e76\u7ee7\u7eed\u8fdb\u884c\u5806\u6392\u5e8f<br \/>    for i in range(len(init_list)):<br \/>        max_heap = max_heap[1:]<br \/>        # \u5982\u679c\u5806\u5927\u5c0f\u53ea\u52691\uff0c\u8f93\u51fa\u6700\u540e\u4e00\u4e2a\u5143\u7d20<br \/>        if len(max_heap) == 1:<br \/>            sort_list.append(max_heap[0])<br \/>            return sort_list<br \/>        # \u83b7\u53d6\u65b0\u5806\uff0c\u5e76\u5c06\u6839\u8282\u70b9\u6dfb\u52a0\u5230\u6392\u5e8f\u5217\u8868\u4e2d<br \/>        sort_list.append(self.build_max_heap(max_heap)[0])<\/pre>\n\n\n<h2 class=\"wp-block-heading\">5.\u4f18\u5148\u961f\u5217<\/h2>\n\n\n<p>\u6211\u4eec\u53ea\u9700\u8981\u6267\u884c\u4e00\u6b21\u6700\u5927\u5806\u64cd\u4f5c\uff08self.build_max_heap(max_heap)\uff09\u5c31\u53ef\u4ee5\u77e5\u9053\u5f53\u524d\u961f\u5217\u4e2d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u5143\u7d20\u662f\u4ec0\u4e48\uff08O(lgn)\uff09\uff0c\u5f53\u6709\u65b0\u7684\u4efb\u52a1\u8fdb\u5165\u961f\u5217\u65f6\uff0c\u6211\u4eec\u4e5f\u53ea\u9700\u8981\u7b80\u5355\u7684\u7ef4\u62a4\u4e00\u4e0b\u6700\u5927\u5806\u5c31\u53ef\u4ee5\u8ba9\u6574\u4e2a\u7cfb\u7edf\u987a\u5229\u8fd0\u884c\uff08O(lgn)\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5806\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(nlgn)\uff0c\u5177\u6709\u539f\u5740\u6027\u3002 \u540c\u65f6\u9700\u8981\u533a\u5206\u201c\u5783\u573e\u6536\u96c6\u5b58\u50a8\u673a\u5236\u201d\u4e2d\u7684\u5806\uff0c\u4f8b\u5982Jav [&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":2270,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2160"}],"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=2160"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2160\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2160"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}