{"id":60200,"date":"2026-01-15T07:41:06","date_gmt":"2026-01-14T23:41:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/60200.html"},"modified":"2026-01-15T07:41:06","modified_gmt":"2026-01-14T23:41:06","slug":"gemini%e6%b0%b8%e4%b9%85%e4%bc%9a%e5%91%98-%e5%a0%86%e6%8e%92%e5%ba%8f-heap-sort-in-go","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/60200.html","title":{"rendered":"Gemini\u6c38\u4e45\u4f1a\u5458    \u5806\u6392\u5e8f (Heap Sort) in Go"},"content":{"rendered":"<p># \u5806\u6392\u5e8f (Heap Sort) in Go<\/p>\n<p>\u5806\u6392\u5e8f\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8c\u53c9\u5806\u6570\u636e\u7ed3\u6784\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Go\u8bed\u8a00\u5b9e\u73b0&#xff1a;<\/p>\n<p>&#096;&#096;&#096;go<br \/>\npackage main<\/p>\n<p>import &#034;fmt&#034;<\/p>\n<p>\/\/ heapSort \u5806\u6392\u5e8f\u4e3b\u51fd\u6570<br \/>\nfunc heapSort(arr []int) {<br \/>\nn :&#061; len(arr)<\/p>\n<p>\/\/ \u6784\u5efa\u6700\u5927\u5806&#xff08;\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u5f00\u59cb&#xff09;<br \/>\nfor i :&#061; n\/2 &#8211; 1; i &gt;&#061; 0; i&#8211; {<br \/>\nheapify(arr, n, i)<br \/>\n}<\/p>\n<p>\/\/ \u4e00\u4e2a\u4e2a\u4ece\u5806\u9876\u53d6\u51fa\u5143\u7d20<br \/>\nfor i :&#061; n &#8211; 1; i &gt; 0; i&#8211; {<br \/>\n\/\/ \u5c06\u5f53\u524d\u5806\u9876&#xff08;\u6700\u5927\u503c&#xff09;\u79fb\u5230\u6570\u7ec4\u672b\u5c3e<br \/>\narr[0], arr[i] &#061; arr[i], arr[0]<\/p>\n<p>\/\/ \u5bf9\u5269\u4f59\u5143\u7d20\u91cd\u65b0\u6784\u5efa\u6700\u5927\u5806<br \/>\nheapify(arr, i, 0)<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ heapify \u7ef4\u62a4\u5806\u7684\u6027\u8d28<br \/>\nfunc heapify(arr []int, n, i int) {<br \/>\nlargest :&#061; i\u00a0\u00a0\u00a0 \/\/ \u521d\u59cb\u5316\u6700\u5927\u503c\u4e3a\u5f53\u524d\u8282\u70b9<br \/>\nleft :&#061; 2*i &#043; 1 \/\/ \u5de6\u5b50\u8282\u70b9<br \/>\nright :&#061; 2*i &#043; 2 \/\/ \u53f3\u5b50\u8282\u70b9<\/p>\n<p>\/\/ \u5982\u679c\u5de6\u5b50\u8282\u70b9\u5b58\u5728\u4e14\u5927\u4e8e\u5f53\u524d\u6700\u5927\u503c<br \/>\nif left &lt; n &amp;&amp; arr[left] &gt; arr[largest] {<br \/>\nlargest &#061; left<br \/>\n}<\/p>\n<p>\/\/ \u5982\u679c\u53f3\u5b50\u8282\u70b9\u5b58\u5728\u4e14\u5927\u4e8e\u5f53\u524d\u6700\u5927\u503c<br \/>\nif right &lt; n &amp;&amp; arr[right] &gt; arr[largest] {<br \/>\nlargest &#061; right<br \/>\n}<\/p>\n<p>\/\/ \u5982\u679c\u6700\u5927\u503c\u4e0d\u662f\u5f53\u524d\u8282\u70b9&#xff0c;\u4ea4\u6362\u5e76\u7ee7\u7eed\u5806\u5316<br \/>\nif largest !&#061; i {<br \/>\narr[i], arr[largest] &#061; arr[largest], arr[i]<br \/>\nheapify(arr, n, largest)<br \/>\n}<br \/>\n}<\/p>\n<p>func main() {<br \/>\narr :&#061; []int{12, 11, 13, 5, 6, 7}<br \/>\nfmt.Println(&#034;\u6392\u5e8f\u524d:&#034;, arr)<\/p>\n<p>heapSort(arr)<br \/>\nfmt.Println(&#034;\u6392\u5e8f\u540e:&#034;, arr)<br \/>\n}<br \/>\n&#096;&#096;&#096;<\/p>\n<p>## \u7b97\u6cd5\u8bf4\u660e<\/p>\n<p>1. **\u6784\u5efa\u6700\u5927\u5806**&#xff1a;\u9996\u5148\u5c06\u65e0\u5e8f\u6570\u7ec4\u6784\u5efa\u6210\u4e00\u4e2a\u6700\u5927\u5806&#xff0c;\u8fd9\u6837\u6700\u5927\u7684\u5143\u7d20\u4f4d\u4e8e\u5806\u9876&#xff08;\u6570\u7ec4\u5f00\u5934&#xff09;\u3002<\/p>\n<p>2. **\u6392\u5e8f\u9636\u6bb5**&#xff1a;<br \/>\n\u00a0\u00a0 &#8211; \u5c06\u5806\u9876\u5143\u7d20&#xff08;\u6700\u5927\u503c&#xff09;\u4e0e\u6570\u7ec4\u672b\u5c3e\u5143\u7d20\u4ea4\u6362<br \/>\n\u00a0\u00a0 &#8211; \u51cf\u5c11\u5806\u7684\u5927\u5c0f&#xff08;\u6392\u9664\u5df2\u6392\u5e8f\u7684\u6700\u5927\u5143\u7d20&#xff09;<br \/>\n\u00a0\u00a0 &#8211; \u5bf9\u65b0\u7684\u5806\u9876\u5143\u7d20\u8fdb\u884c\u5806\u5316&#xff0c;\u4f7f\u5176\u6ee1\u8db3\u6700\u5927\u5806\u6027\u8d28<br \/>\n\u00a0\u00a0 &#8211; \u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\u76f4\u5230\u5806\u7684\u5927\u5c0f\u4e3a1<\/p>\n<p>## \u65f6\u95f4\u590d\u6742\u5ea6<\/p>\n<p>&#8211; \u6784\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O(n)<br \/>\n&#8211; \u6bcf\u6b21\u5806\u5316\u7684\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O(log n)<br \/>\n&#8211; \u603b\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O(n log n)<br \/>\n&#8211; \u7a7a\u95f4\u590d\u6742\u5ea6&#xff1a;O(1)&#xff08;\u539f\u5730\u6392\u5e8f&#xff09;<\/p>\n<p>## \u7279\u70b9<\/p>\n<p>&#8211; \u4e0d\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5<br \/>\n&#8211; \u9002\u5408\u5927\u6570\u636e\u91cf\u7684\u6392\u5e8f<br \/>\n&#8211; \u4e0d\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4&#xff08;\u539f\u5730\u6392\u5e8f&#xff09;<br \/>\n&#8211; \u5b9e\u9645\u5e94\u7528\u4e2d\u6bd4\u5feb\u901f\u6392\u5e8f\u6162&#xff0c;\u4f46\u6700\u574f\u60c5\u51b5\u4e0b\u6027\u80fd\u66f4\u597d&#xff08;O(n log n) vs \u5feb\u901f\u6392\u5e8f\u7684O(n\u00b2)&#xff09;<\/p>\n<p>\u8fd9\u4e2a\u5b9e\u73b0\u4f7f\u7528\u4e86\u9012\u5f52\u7684\u5806\u5316\u65b9\u6cd5&#xff0c;\u4e5f\u53ef\u4ee5\u6539\u4e3a\u8fed\u4ee3\u5b9e\u73b0\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<br \/>\n\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p># \u5806\u6392\u5e8f (Heap Sort) in Go\u5806\u6392\u5e8f\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8c\u53c9\u5806\u6570\u636e\u7ed3\u6784\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Go\u8bed\u8a00\u5b9e\u73b0&#xff1a;go<br \/>\npackage mainimport \\&#8221;fmt\\&#8221;\/\/ heapSort \u5806\u6392\u5e8f\u4e3b\u51fd\u6570<br \/>\nfunc heapSort(arr []int) {<br \/>\nn : len(arr)\/\/ \u6784\u5efa\u6700\u5927\u5806&#xff08;\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u5f00\u59cb&#xff09;<br \/>\nfor i : n\/2 &#8211; 1; i &gt; 0; i&#8211; {<br \/>\nheapify(arr, n, i)<br \/>\n}<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[746,292,1813,427],"topic":[],"class_list":["post-60200","post","type-post","status-publish","format-standard","hentry","category-server","tag-golang","tag-292","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Gemini\u6c38\u4e45\u4f1a\u5458  \u5806\u6392\u5e8f (Heap Sort) in Go - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/60200.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Gemini\u6c38\u4e45\u4f1a\u5458  \u5806\u6392\u5e8f (Heap Sort) in Go - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"# \u5806\u6392\u5e8f (Heap Sort) in Go\u5806\u6392\u5e8f\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8c\u53c9\u5806\u6570\u636e\u7ed3\u6784\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Go\u8bed\u8a00\u5b9e\u73b0&#xff1a;go package mainimport &quot;fmt&quot;\/\/ heapSort \u5806\u6392\u5e8f\u4e3b\u51fd\u6570 func heapSort(arr []int) { n : len(arr)\/\/ \u6784\u5efa\u6700\u5927\u5806&#xff08;\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u5f00\u59cb&#xff09; for i : n\/2 - 1; i &gt; 0; i-- { heapify(arr, n, i) }\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/60200.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-14T23:41:06+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60200.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/60200.html\",\"name\":\"Gemini\u6c38\u4e45\u4f1a\u5458 \u5806\u6392\u5e8f (Heap Sort) in Go - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-14T23:41:06+00:00\",\"dateModified\":\"2026-01-14T23:41:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/60200.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/60200.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60200.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Gemini\u6c38\u4e45\u4f1a\u5458 \u5806\u6392\u5e8f (Heap Sort) in Go\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Gemini\u6c38\u4e45\u4f1a\u5458  \u5806\u6392\u5e8f (Heap Sort) in Go - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/60200.html","og_locale":"zh_CN","og_type":"article","og_title":"Gemini\u6c38\u4e45\u4f1a\u5458  \u5806\u6392\u5e8f (Heap Sort) in Go - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"# \u5806\u6392\u5e8f (Heap Sort) in Go\u5806\u6392\u5e8f\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8c\u53c9\u5806\u6570\u636e\u7ed3\u6784\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Go\u8bed\u8a00\u5b9e\u73b0&#xff1a;go package mainimport \"fmt\"\/\/ heapSort \u5806\u6392\u5e8f\u4e3b\u51fd\u6570 func heapSort(arr []int) { n : len(arr)\/\/ \u6784\u5efa\u6700\u5927\u5806&#xff08;\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u5f00\u59cb&#xff09; for i : n\/2 - 1; i &gt; 0; i-- { heapify(arr, n, i) }","og_url":"https:\/\/www.wsisp.com\/helps\/60200.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-14T23:41:06+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/60200.html","url":"https:\/\/www.wsisp.com\/helps\/60200.html","name":"Gemini\u6c38\u4e45\u4f1a\u5458 \u5806\u6392\u5e8f (Heap Sort) in Go - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-14T23:41:06+00:00","dateModified":"2026-01-14T23:41:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/60200.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/60200.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/60200.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Gemini\u6c38\u4e45\u4f1a\u5458 \u5806\u6392\u5e8f (Heap Sort) in Go"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/60200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=60200"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/60200\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=60200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=60200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=60200"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=60200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}