{"id":4042,"date":"2019-12-23T16:04:41","date_gmt":"2019-12-23T08:04:41","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4042"},"modified":"2019-12-23T16:04:41","modified_gmt":"2019-12-23T08:04:41","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3%e5%a0%86%ef%bc%88%e6%9c%80%e5%a4%a7%e5%a0%86%ef%bc%8c%e6%9c%80%e5%b0%8f%e5%a0%86%e5%8f%8a%e5%a0%86%e6%8e%92%e5%ba%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4042","title":{"rendered":"\u6df1\u5165\u7406\u89e3\u5806\uff08\u6700\u5927\u5806\uff0c\u6700\u5c0f\u5806\u53ca\u5806\u6392\u5e8f\uff09"},"content":{"rendered":"<p><a href=\"https:\/\/blog.csdn.net\/summerlq\/article\/details\/82747940\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u57fa\u672c\u6982\u5ff5\uff1a<\/p>\n<p>1\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\uff1a\u82e5\u4e8c\u53c9\u6811\u7684\u6df1\u5ea6\u4e3ah\uff0c\u5219\u9664\u7b2ch\u5c42\u5916\uff0c\u5176\u4ed6\u5c42\u7684\u7ed3\u70b9\u5168\u90e8\u8fbe\u5230\u6700\u5927\u503c\uff0c\u4e14\u7b2ch\u5c42\u7684\u6240\u6709\u7ed3\u70b9\u90fd\u96c6\u4e2d\u5728\u5de6\u5b50\u6811\u3002<\/p>\n<p>2\u3001\u6ee1\u4e8c\u53c9\u6811\uff1a\u6ee1\u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6240\u6709\u5c42\u7684\u7ed3\u70b9\u90fd\u662f\u6700\u5927\u503c\u3002<\/p>\n<p>\u5b9a\u4e49\uff1a<br \/>\n1\u3001\u5806\u662f\u4e00\u9897\u5b8c\u5168\u4e8c\u53c9\u6811\uff1b<\/p>\n<p>2\u3001\u5806\u4e2d\u7684\u67d0\u4e2a\u7ed3\u70b9\u7684\u503c\u603b\u662f\u5927\u4e8e\u7b49\u4e8e\uff08\u6700\u5927\u5806\uff09\u6216\u5c0f\u4e8e\u7b49\u4e8e\uff08\u6700\u5c0f\u5806\uff09\u5176\u5b69\u5b50\u7ed3\u70b9\u7684\u503c\u3002<\/p>\n<p>3\u3001\u5806\u4e2d\u6bcf\u4e2a\u7ed3\u70b9\u7684\u5b50\u6811\u90fd\u662f\u5806\u6811\u3002<\/p>\n<p>\u5806\u7684\u64cd\u4f5c<br \/>\n\u5047\u8bbe\u539f\u59cb\u6570\u636e\u4e3aa[]={4,1,3,2,16,9.10.14.8.7},\u91c7\u7528\u987a\u5e8f\u5b58\u50a8\u5bf9\u5e94\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\u4e3a\uff1a<\/p>\n<p>\u5806\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b<\/p>\n<p>struct MaxHeap<br \/>\n{<br \/>\nEType *heap; \/\/\u5b58\u653e\u6570\u636e\u7684\u7a7a\u95f4\uff0c\u4e0b\u6807\u4ece1\u5f00\u59cb\u5b58\u50a8\u6570\u636e\uff0c\u4e0b\u6807\u4e3a0\u7684\u4f5c\u4e3a\u5de5\u4f5c\u7a7a\u95f4\uff0c\u5b58\u50a8\u4e34\u65f6\u6570\u636e\u3002<br \/>\nint MaxSize; \/\/MaxSize\u662f\u5b58\u653e\u6570\u636e\u5143\u7d20\u7a7a\u95f4\u7684\u5927\u5c0f<br \/>\nint HeapSize; \/\/HeapSize\u662f\u6570\u636e\u5143\u7d20\u7684\u4e2a\u6570<br \/>\n};<br \/>\nMaxHeap H;<br \/>\n1\u3001\u6784\u9020\u6700\u5927\u5806<\/p>\n<p>\u57fa\u672c\u601d\u60f3\uff1a\u9996\u5148\u5c06\u6bcf\u4e2a\u53f6\u5b50\u7ed3\u70b9\u89c6\u4e3a\u4e00\u4e2a\u5806\uff0c\u518d\u5c06\u6bcf\u4e2a\u53f6\u5b50\u7ed3\u70b9\u4e8e\u5176\u7236\u8282\u70b9\u4e00\u8d77\u6784\u6210\u4e00\u4e2a\u5305\u542b\u66f4\u591a\u7ed3\u70b9\u7684\u5806\u3002\u6240\u4ee5\u5728\u6784\u9020\u5806\u7684\u65f6\u5019\uff0c\u9996\u5148\u9700\u8981\u627e\u5230\u6700\u540e\u4e00\u4e2a\u7ed3\u70b9\u7684\u7236\u8282\u70b9\uff0c\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\u6784\u9020\u6700\u5927\u5806\uff0c\u76f4\u5230\u8be5\u8282\u70b9\u524d\u9762\u7684\u6240\u6709\u5206\u652f\u8282\u70b9\u90fd\u5904\u7406\u5b8c\u6bd5\u3002<\/p>\n<p>\u6ce8\u610f\uff1a \u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u82e5\u5f53\u524d\u8282\u70b9\u7684\u4e0b\u6807\u4e3a i\uff0c \u5219\u5176\u7236\u8282\u70b9\u7684\u4e0b\u6807\u4e3a i\/2\uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7684\u4e0b\u6807\u4e3a i*2\uff0c\u5176\u53f3\u5b50\u8282\u70b9\u7684\u4e0b\u6807\u4e3ai*2+1\uff1b<\/p>\n<p>2\u3001\u521d\u59cb\u5316\u5806<\/p>\n<p>void MaxHeapInit(MaxHeap &amp;H)<br \/>\n{<br \/>\nfor(int i=H.HeapSize\/2;i&gt;=1;i&#8211;)<br \/>\n{<br \/>\nH.heap[0]=H.heap[i];<br \/>\nint son=i*2;<br \/>\nwhile(son&lt;H.HeapSize)<br \/>\n{<br \/>\nif(son&lt;H.HeapSize&amp;&amp;H.heap[son]&lt;H.heap[son+1])<br \/>\nson++;<br \/>\nif(H.heap[i]&gt;H.heap[son])<br \/>\nbreak;<br \/>\nelse if(son&lt;H.heapSize&amp;&amp;H.heap[son]&gt;H.heap[son+1]<br \/>\n{<br \/>\nH.heap[son\/2]=H.heap[son];<br \/>\nson*=2;<br \/>\n}<br \/>\n}<br \/>\nH.heap[son\/2]=H.heap[0];<br \/>\n}<br \/>\n}<br \/>\n\u4e0b\u56fe\u662f\u539f\u59cb\u6570\u636e\u5806\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>3\u3001\u6700\u5927\u5806\u4e2d\u63d2\u5165\u8282\u70b9<\/p>\n<p>\u6700\u5927\u5806\u4e2d\u63d2\u5165\u8282\u70b9\uff0c\u5148\u5728\u5806\u672b\u5c3e\u63d2\u5165\u8be5\u8282\u70b9\uff0c\u7136\u540e\u6309\u7167\u5806\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u5c06\u8be5\u8282\u70b9\u653e\u5165\u5230\u5408\u9002\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>void MaxHeapInsert(MaxHeap &amp;H, EType &amp;x)<br \/>\n{<br \/>\nif(H.HeapSize==H.MaxSize) return false;<\/p>\n<p>int i=++H.HeapSize;<br \/>\nwhile(i!=1&amp;&amp;x&gt;H.heap[i\/2])<br \/>\n{<br \/>\nH.heap[i]=H.heap[i\/2];<br \/>\ni\/=2;<br \/>\n}<br \/>\nH.heap[i]=x;<br \/>\nreturn true;<br \/>\n}<br \/>\n4\\\u6700\u5927\u5806\u4e2d\u5220\u9664\u8282\u70b9<\/p>\n<p>\u5c06\u6700\u5927\u5806\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u653e\u5230\u6839\u8282\u70b9\uff0c\u7136\u540e\u5220\u9664\u6700\u5927\u503c\uff0c\u7136\u540e\u518d\u628a\u65b0\u7684\u6839\u8282\u70b9\u653e\u5230\u5408\u9002\u7684\u4f4d\u7f6e<\/p>\n<p>void MaxHeapDelete(MaxHeap &amp;H, EType &amp;x)<br \/>\n{<br \/>\nif(H.HeapSize==0) return false;<br \/>\nx=H.heap[1];<br \/>\nH.heap[0]=H.heap[H.HeapSize&#8211;];<br \/>\nint i=1, son=i*2;<br \/>\nwhile(son&lt;H.HeapSize)<br \/>\n{<br \/>\nif(son&lt;H.HeapSize&amp;&amp;H.heap[son]&lt;H.heap[son+1])<br \/>\nson++;<br \/>\nif(H.heap[i]&gt;H.heap[son])<br \/>\nbreak;<br \/>\nH.heao[i]=H.heap[son];<br \/>\ni=son;<br \/>\nson*=2;<br \/>\n}<br \/>\nH.heap[i]=H.heap[0];<br \/>\nreturn true;<\/p>\n<p>}<br \/>\n5\u3001\u5806\u6392\u5e8f<\/p>\n<p>#include&lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>void swap(int &amp;a, int &amp;b)<br \/>\n{<br \/>\nint temp=a;<br \/>\na=b;<br \/>\nb=temp;<br \/>\n}<\/p>\n<p>void quick_build(int a[], int len, int root)<br \/>\n{<br \/>\nint left=root*2+1;<br \/>\nint flag=left;<br \/>\nwhile(left&lt;len)<br \/>\n{<br \/>\nint right=left+1;<br \/>\nwhile(right&lt;len&amp;&amp;a[right]&gt;a[left])<br \/>\nflag=right;<\/p>\n<p>}<br \/>\nif(a[root]&lt;a[flag])<br \/>\n{<br \/>\nswap(a[root],a[flag]);<br \/>\nheap_build(a,len,flag);<br \/>\n}<\/p>\n<p>void quick_sort(int a[], int len)<br \/>\n{<br \/>\nfor(int i=len\/2;i&gt;0;i&#8211;)<br \/>\nheap_build(a,len, i);<br \/>\nfor(int j=len-1;j&gt;0;j&#8211;)<br \/>\n{<br \/>\nswap(a[0],a[j]);<br \/>\nheap_build(a,0,j);<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br \/>\n\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300csummerlq\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faa CC 4.0 BY-SA \u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002<br \/>\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/summerlq\/article\/details\/82747940<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; &nbsp; \u57fa<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4042","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4042"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4042\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}