{"id":1587,"date":"2017-05-18T16:07:45","date_gmt":"2017-05-18T07:07:45","guid":{"rendered":"http:\/\/43.203.250.216\/?p=1587"},"modified":"2025-10-01T16:25:01","modified_gmt":"2025-10-01T07:25:01","slug":"heap-sort-%ed%9e%99-%ec%a0%95%eb%a0%ac","status":"publish","type":"post","link":"https:\/\/litcoder.com\/?p=1587","title":{"rendered":"Heap sort (\ud799 \uc815\ub82c)"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>Heap sort\ub294 root node\uc5d0 \ud56d\uc0c1 \uc81c\uc77c \uc791\uc740 \uac12\uc774\ub098 \uc81c\uc77c \ud070 \uac12\uc774 \uc624\ub3c4\ub85d heap tree\ub97c \uad6c\uc131\ud574\uc11c \uc815\ub82c\ud558\ub294 \ubc29\ubc95\uc774\ub2e4. Heap tree\uc758 root\uc5d0 \ucd5c\uc18c&nbsp;\uac12\uc774 \uc624\ub3c4\ub85d \uad6c\ud604\ud558\uba74 min heap tree, \ucd5c\ub300 \uac12\uc774 \uc624\ub3c4\ub85d \ud558\uba74 max heap tree\ub77c \ud55c\ub2e4. Heap tree\ub97c \uad6c\uc131\ud558\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574\uc11c\ub294 \ubcc4\ub3c4\uc758 \ud3ec\uc2a4\ud2b8\ub85c \ub2e4\ub8e8\uace0 \uc5ec\uae30\uc11c\ub294 max heap\uc73c\ub85c \uc624\ub984\ucc28\uc21c \uc815\ub82c\ud558\ub294 \uacfc\uc815\uc744 \uc124\uba85\ud55c\ub2e4.<\/em><\/p><\/blockquote>\n\n\n\n<p>Heap sort\ub294 \ud55c\ubc88 heap tree\ub97c \uad6c\uc131\ud55c \ub2e4\uc74c\uc5d0\ub294, \uc5b4\ub5a4 \uac12\uc774 \uc81c\uc790\ub9ac\ub97c \ucc3e\ub294\ub370 \uc18c\uc694\ub418\ub294 \uc2dc\uac04\uc774 \uc9e7\ub2e4\ub294 \uc810\uc744 \uc774\uc6a9\ud55c\ub2e4. \ubd80\ubaa8\uc640 \uc790\uc2dd\ub178\ub4dc\uc758 \uac12\uc5d0 \ub530\ub77c \uc644\uc804 \uc774\uc9c4\ud2b8\ub9ac\ub85c \uad6c\uc131\ud558\ubbc0\ub85c, \ucc98\uc74c \ud55c \ubc88 heap tree\ub97c \uad6c\uc131\ud560 \ub54c O(N\/2)\ub9cc\ud07c \uc18c\uc694\ub41c \uc774\ud6c4\uc5d0\ub294, heap tree\uac00 \uad6c\uc131\ub41c \uc0c1\ud0dc\uc5d0\uc11c\ub294 \uc5b4\ub5a4 \ub178\ub4dc\uc758 \uc81c\uc790\ub9ac\ub97c \ucc3e\ub294\ub370 O(log N) \ub9cc\ud07c\uc774 \uc18c\uc694\ub41c\ub2e4.<\/p>\n\n\n\n<p>Heap sort \uacfc\uc815\uc744 \uc815\ub9ac\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1. Heap tree\ub97c \ub9cc\ub4e0\ub2e4.\n2. \uc81c\uc77c \ub9c8\uc9c0\ub9c9 \ub178\ub4dc\ub97c root\uc640 \ubc14\uafd4\uc11c heap tree\ub978 \ub9dd\uac00\ub728\ub9b0\ub2e4. \n   \uc774\ub54c, root\uc5d0 \uc788\ub358 \ucd5c\ub300\uac12\uc740 \ubc30\uc5f4\uc758 \ub9c8\uc9c0\ub9c9 \uc704\uce58\ub85c \uc62e\uaca8\uc9c4\ub2e4.\n3. Heap tree\ub97c \uad6c\uc131\ud558\ub294 \ubc30\uc5f4 \ud06c\uae30\ub97c \ud558\ub098 \uc904\uc5ec\uc11c \uc62e\uaca8\uc9c4 \ucd5c\ub300\uac12\uc744 heap tree\uc5d0\uc11c \uc81c\uc678\uc2dc\ud0a8\ub2e4.\n4. \ub2e4\uc2dc heap tree\uac00 \ub418\ub3c4\ub85d \uc0c8\ub85c\uc6b4 root\uc758 \uc790\ub9ac\ub97c \ucc3e\uc544 \uc900\ub2e4.\n5. \uc704\uc758 2, 3, 4\ubc88 \uacfc\uc815\uc744 \ubc30\uc5f4\uc758 \ud06c\uae30\uac00 1\uc774 \ub420\ub54c \uae4c\uc9c0 \ubc18\ubcf5\ud55c\ub2e4.<\/pre>\n\n\n\n<p>\uc5b8\ub73b \uc798 \uc774\ud574\uac00 \uc548 \uac08\uc218\ub3c4 \uc788\ub294\ub370, <strong>\ubaa8\uc885\uc758 \uacfc\uc815\uc744 \uac70\uce58\uace0 \ub098\uba74<\/strong> root (\ubc30\uc5f4\uc758 0\ubc88 index)\uc5d0 \ucd5c\ub300\uac12\uc774 \uc624\uac8c\ub418\ub294 \ub9c8\ubc95\uc758 tree\uac00 \uc788\uace0 \uc774\uac83\uc758 root\ub97c \uc2f9\ub451\uc2f9\ub451 \uc790\ub97c\ub54c\ub9c8\ub2e4 \ub0a8\uc544\uc788\ub294 \uac83\ub4e4 \uc911 \uc81c\uc77c \ud070\uac12\uc774 root\uc5d0 \uc62c\ub77c\uc624\ub294 \uac83\uc744 \uc0c1\uc0c1\ud574\ubcf4\uba74 \ub3c4\uc6c0\uc774 \ub420\uc9c0\ub3c4 \ubaa8\ub974\uaca0\ub2e4. Tree\ub77c\uace0 \ud558\uba74 \uc77c\ubc18\uc801\uc73c\ub85c \uc67c\ucabd\uacfc \uc624\ub978\ucabd \uc790\uc2dd\ub178\ub4dc\uc5d0 \ub300\ud55c pointer\ub97c \uac00\uc9c0\ub294 \ud615\ud0dc\ub97c \ub5a0\uc62c\ub9ac\uac8c \ub418\ub2c8\uae4c \ubc30\uc5f4\ub85c \ud45c\ud604\ub41c tree\uac00 \ud63c\ub3d9\uc2a4\ub7ec\uc6b8 \uc218\ub3c4 \uc788\uaca0\ub294\ub370, \uc644\uc804 \uc774\uc9c4 \ud2b8\ub9ac\uc778 \uacbd\uc6b0\uc5d0\ub294 \ubc30\uc5f4\uc548\uc5d0 \uc6d0\uc18c\ub4e4\uc774 \ucc28\ub840\ub300\ub85c \ub4e4\uc5b4 \uc788\ub2e4\uace0 \uac00\uc815\ud560 \ub54c \uc67c\ucabd\/\uc624\ub978\ucabd \uc790\uc2dd node\uc758 \uc704\uce58\ub97c \ub2e4\uc74c\uc758 \uacc4\uc0b0\uc2dd\uc73c\ub85c \uad6c\ud560 \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\uc67c\ucabd \uc790\uc2dd index = (2 * \ubd80\ubaa8 node index) + 1\n\uc624\ub978\ucabd \uc790\uc2dd index = \uc67c\ucabd \uc790\uc2dd index + 1<\/pre>\n\n\n\n<p>\uadf8\ub9ac\uace0 \ud558\ub098\ub77c\ub3c4 \uc790\uc2dd\uc774 \uc788\ub294 \ubd80\ubaa8 node\ub4e4\uc758 index\ub3c4 \uad6c\ud560 \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\ubd80\ubaa8 node\ub4e4 = [0 \ubd80\ud130 floor(\ubc30\uc5f4 \ud06c\uae30 \/ 2) - 1 \uae4c\uc9c0]\n\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted theme:coda-special-board lang:default highlight:0 decode:true\">* floor() : \uc18c\uc22b\uc810 \ubc84\ub9bc\n\uc608) \ubc30\uc5f4\ud06c\uae30\uac00 10 \uc774\uba74 \ubd80\ubaa8 node\ub294 0, 1, 2, 3, 4\uac00 \ub41c\ub2e4.<\/pre>\n\n\n\n<p>\uc5b4\ub5a4 \ubc30\uc5f4\uc774 \ub2e4\uc74c\uc758 \uc21c\uc11c\uc77c \ub54c \uc774\ub97c heap sort\ud558\ub294 \uacfc\uc815\uc758 \uc608\ub97c \ub4e4\uc5b4 \uc0b4\ud3b4\ubcf4\uc790.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted theme:coda-special-board lang:default highlight:0 decode:true\">{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}<\/pre>\n\n\n\n<p><em>1. &#8220;Heap tree\ub97c \ub9cc\ub4e0\ub2e4.&#8221;<\/em><\/p>\n\n\n\n<p>Max heap\uc744 \uac00\uc815\ud558\ubbc0\ub85c, \uc704\uc758 \ubc30\uc5f4\ub85c max heap tree\ub97c \ub9cc\ub4e4\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4. \ub300\uad04\ud638([ ]) \uc548\uc5d0 \uc788\ub294 \uc22b\uc790\ub294 \ubc30\uc5f4\uc0c1\uc758 &nbsp;index\ub97c \uc758\ubbf8\ud55c\ub2e4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_build_heap-2.png\" alt=\"\" class=\"wp-image-1592\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_build_heap-2.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_build_heap-2-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_build_heap-2-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p>\uc608\uc81c\uc758 \uacbd\uc6b0, \uc774\ubbf8 &#8220;\ubaa8\ub4e0 \uc11c\ube0c\ud2b8\ub9ac\uc758 \ubd80\ubaa8\uac00 \uac00\uc9c4 \uac12\uc774 \uc790\uc2dd\ubcf4\ub2e4 \ud06c\ub2e4&#8221;\ub294 max heap tree \uc870\uac74\uc744 \ub9cc\uc871\ud558\uae30 \ub54c\ubb38\uc5d0 \ubc30\uc5f4 \uc548\uc5d0\uc11c\uc758 \uc21c\uc11c\uac00 \ubcc0\uacbd\ub418\uc9c0 \uc54a\ub294\ub2e4. \ub2e4\ub978 \uacbd\uc6b0\uc5d0 heap tree\ub97c \uc0dd\uc131\ud558\ub294 \uc808\ucc28\ub294 \ub098\uc911\uc5d0 \uc124\uba85\ud558\ub3c4\ub85d \ud558\uace0 \uc9c0\uae08\uc740 \uaf3c\uc218 \uac19\uaca0\uc9c0\ub9cc \uc77c\ub2e8 \uadf8\ub0e5 \ub118\uc5b4\uac00\uc790. \uc704 \uadf8\ub9bc\uc5d0\uc11c \uc0c9\uce60\ub41c node\ub4e4\uc740 \uc790\uc2dd\ub4e4\uc744 \uac00\uc9c0\ub294 \ubd80\ubaa8 node\ub4e4\uc778\ub370, \uc55e\uc11c \uc124\uba85\ud55c \ubd80\ubaa8 node\ub4e4\uc758 index\ub97c \uad6c\ud558\ub294 \uacf5\uc2dd\uc5d0 \uc758\ud574\uc11c \uc5bb\uc744 \uc218 \uc788\ub2e4. \uc774 \uc608\uc81c\uc5d0\uc11c\ub294 \uc798 \ubcf4\uc774\uc9c0 \uc54a\uc9c0\ub9cc, \ubd80\ubaa8 node\ub4e4\uc758 index\ub97c \uad6c\ud558\ub294 \uac83\uc740 heap tree\ub97c \uad6c\uc131\ud560 \ub54c \uc911\uc694\ud558\ub2e4. \uc774 \ubd80\ubd84\ub3c4 \ubcc4\ub3c4\uc758 \ud3ec\uc2a4\ud305\uc73c\ub85c \ub530\ub85c \uc124\uba85\ud55c\ub2e4.<\/p>\n\n\n\n<p><em>2. &#8220;\uc81c\uc77c \ub9c8\uc9c0\ub9c9 node\ub97c root\uc640 \ubc14\uafd4\uc11c heap tree\ub97c \ub9dd\uac00\ub728\ub9b0\ub2e4.&#8221;<\/em><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1.png\" alt=\"\" class=\"wp-image-1594\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p><em>&#8220;\uc544\ub2c8 \uc65c?&#8221;<\/em> \ud558\ub294 \uc0dd\uac01\uc774 \ub4e4 \uc218\ub3c4 \uc788\uc744 \ud150\ub370, root\uc5d0\ub294 \ucd5c\ub300\uac12\uc774 \ub4e4\uc5b4 \uc788\uc73c\ubbc0\ub85c \uc81c\uc77c \ub9c8\uc9c0\ub9c9 node\uc640 swap\ud558\uba74 root\uc5d0 \uc788\ub358 \uac12\uc740 \uc624\ub984 \ucc28\uc21c \uc815\ub82c\uc77c \ub54c\uc758 \uc790\uae30 \uc790\ub9ac\ub97c \ucc3e\uc544\uac00\ub294 \uc0d8\uc774 \ub41c\ub2e4.<\/p>\n\n\n\n<p><em>3. &#8220;Heap tree\ub97c \uad6c\uc131\ud558\ub294 \ubc30\uc5f4\uc758 \ud06c\uae30\ub97c \ud558\ub098 \uc904\uc778\ub2e4.&#8221;<\/em><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_1.png\" alt=\"\" class=\"wp-image-1595\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_1.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_1-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_1-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p>\uc774\uc804\uc758 root\uc600\ub358 10\uc740 \uc81c\uc790\ub9ac\ub97c \ucc3e\uc558\uc73c\ub2c8 \uc774\uc81c \ubc30\uc5f4\uc758 \ud06c\uae30\ub97c \ud558\ub098 \uc904\uc5ec\uc11c index 0 ~ 8\uae4c\uc9c0\uac00 \ubc30\uc5f4\uc774\ub77c\uace0 \uac00\uc815\ud55c\ub2e4. \ubc30\uc5f4 \uadf8\ub9bc\uc744 \ubcf4\uba74 \uc815\ub82c\ub418\uc5b4\uac00\ub294 \ubaa8\uc2b5\uc774 \ubcf4\uc77c \uac83\uc774\ub2e4.<\/p>\n\n\n\n<p><em>4. &#8220;\ub2e4\uc2dc heap tree\uac00 \ub418\ub3c4\ub85d \uc0c8\ub85c\uc6b4 root\uc758 \uc790\ub9ac\ub97c \ucc3e\uc544 \uc900\ub2e4.&#8221;<\/em><\/p>\n\n\n\n<p>\uc5bc\ub5a8\uacb0\uc5d0 \uc0c8\ub85c\uc6b4 root\uac00 \ub41c node\ub294 \uc774\uc81c \uc790\uae30 \uc788\uc744 \uacf3\uc744 \ucc3e\uc544\uac00\uc57c \ud55c\ub2e4. \uc790\uc2dd node\ub4e4\uacfc\uc758 \ud06c\uae30\ub97c \ube44\uad50\ud574\uc11c \ub458 \uc911 \ud070 \uc790\uc2dd\uacfc swap\ud558\uba74\uc11c \uc6d0\ub798 \uc788\uc5b4\uc57c \ud560 \uacf3\uc744 \ucc3e\uc544\uac04\ub2e4. \uc644\uc804 \uc774\uc9c4 \ud2b8\ub9ac\uc758 \uc7a5\uc810\uc744 \uc0b4\ub824\uc11c O(log N)\uc758 \ube44\uad50 \uc5f0\uc0b0\ub9cc\uc5d0 \uc790\uae30 \uc790\ub9ac\ub97c \ucc3e\uc744 \uc218 \uc788\ub2e4. \uc774 \uacfc\uc815\uc5d0\uc11c \ub178\ub4dc\ub4e4\uac04\uc758 swap\uc774 \uc77c\uc5b4\ub098\uace0 root\uc5d0\ub294 \ub2e4\uc2dc \ucd5c\uc18c \ud639\uc740 \ucd5c\ub300\uac12\uc774 \uc624\uac8c\ub41c\ub2e4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_2.png\" alt=\"\" class=\"wp-image-1596\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_2.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_2-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_2-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-left\">Root\uc600\ub358 &#8216;1&#8217;\uc740 \uc790\uc2dd\ub4e4 \uc911 \ub354 \ud070 \uc67c\ucabd \uc790\uc2dd\uc778 &#8216;9&#8217;\uc640 \uc790\ub9ac\ub97c \ubc14\uafbc\ub2e4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_3.png\" alt=\"\" class=\"wp-image-1597\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_3.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_3-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_3-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-left\">\uc5ec\uc804\ud788 max heap tree\ub97c \ub9cc\uc871\ud558\uc9c0 \ubabb\ud558\ubbc0\ub85c &#8216;7&#8217;, &#8216;6&#8217; \uc911 \ud070 \uc790\uc2dd\uc778 &#8216;7&#8217;\uacfc \uc790\ub9ac\ub97c \ubc14\uafbc\ub2e4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_4.png\" alt=\"\" class=\"wp-image-1598\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_4.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_4-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap1_4-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p>\ub9c8\uc9c0\ub9c9\uc73c\ub85c &#8216;3&#8217;, &#8216;2&#8217; \uc911 \ud070 \uc790\uc2dd\uc778 &#8216;3&#8217;\uacfc \uc790\ub9ac\ub97c \ubc14\uafb8\uba74 \ubaa8\ub4e0 \uc11c\ube0c\ud2b8\ub9ac\uac00 max heap tree\uc758 \uc870\uac74\uc744 \ub9cc\uc871\ud558\ub294 \uc0c1\ud0dc\uac00 \ub41c\ub2e4.<\/p>\n\n\n\n<p><em>5. &#8220;\uc704\uc758 2, 3, 4\ubc88 \uacfc\uc815\uc744 \ubc30\uc5f4\uc758 \ud06c\uae30\uac00 1\uc774 \ub420\ub54c \uae4c\uc9c0 \ubc18\ubcf5\ud55c\ub2e4.&#8221;<\/em><\/p>\n\n\n\n<p>\uc774\uc81c \ub2e4\uc2dc \uc0c8\ub85c\ubf51\ud78c root\uc640 \ub9c8\uc9c0\ub9c9 node\uc778 2\ub97c&nbsp;swap\ud558\uace0 \ubc30\uc5f4 \ud06c\uae30\ub97c \ud558\ub098 \uc904\uc778 \ub2e4\uc74c \uc0c8\ub85c\uc6b4 node\uc758 \uc790\ub9ac\ub97c \ucc3e\uc544\uc900\ub2e4. \uc989, node\ub97c swap\ud558\uace0 index\ub97c \ud558\ub098 \uc904\uc774\ub294 \uacfc\uc815\uc744 O(N)\ubc88 \ubc18\ubcf5 \ud558\uace0 \uac01 \ubc18\ubcf5 \ub9c8\ub2e4 O(log N)\uc758 &#8211;\uc815\ud655\ud788\ub294 \ub9e4 \ubc18\ubcf5 \ub9c8\ub2e4 \ubc30\uc5f4\uc758 \ud06c\uae30\uac00 \ud558\ub098\uc529 \uc904\uc5b4\ub4ec&#8211; \ube44\uad50 \uc5f0\uc0b0\uc774 \ud544\uc694\ud558\ub2e4.&nbsp;&nbsp;\ud55c\ubc14\ud034 \ub354 \ub3cc\uc544 \ubcf4\uba74.<\/p>\n\n\n\n<p>\ubc14\uafb8\uace0<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_1.png\" alt=\"\" class=\"wp-image-1602\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_1.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_1-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_1-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p>\ud06c\uae30\uc904\uc774\uace0<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_2.png\" alt=\"\" class=\"wp-image-1601\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_2.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_2-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_2-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p>\uc790\ub9ac \ucc3e\uc544\uc8fc\uace0<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_3.png\" alt=\"\" class=\"wp-image-1600\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_3.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_3-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_3-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_4.png\" alt=\"\" class=\"wp-image-1599\" srcset=\"https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_4.png 960w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_4-300x225.png 300w, https:\/\/litcoder.com\/wp-content\/uploads\/2017\/05\/heaptreeEX_rootswap2_4-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure><\/div>\n\n\n\n<p>\uc774\ub807\uac8c \uc6d0\uc18c\ub97c \ud558\ub098\uc529 \uc904\uc5ec \uac00\uba74\uc11c 1\uac1c\uac00 \ub0a8\uc744\ub54c\uae4c\uc9c0 \ubc18\ubcf5\ud558\uba74 \uc815\ub82c\ub41c \ubc30\uc5f4\uc744 \uc5bb\uac8c \ub41c\ub2e4.<\/p>\n\n\n\n<p>Code\ub85c \ub098\ud0c0\ub0b4\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4. (\ucda9\ubd84\ud788 \uc2dc\ud5d8\ub418\uc9c0 \uc54a\uc558\uc74c)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"eclipse\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Name: heapSort\n  Desc.: Sorts given array 'arr' as an increasing order.\n  Args.:\n    - arr : Pointer to array to be sorted.\n    - arrSize : Size of the array. (Number of elements in the array)\n*\/\nvoid heapSort(int* arr, int arrSize)\n{\n    int arrLastIdx = arrSize - 1;\n    buildHeapTree(arr, arrSize);\n    while(arrLastIdx > 0)\n    {\n        swap(arr, 0, arrLastIdx);\n        arrLastIdx --;\n        maxHeapify(arr, arrLastIdx, 0);\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"eclipse\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Name: swap\n  Desc.: Exchanges two elements of the array 'arr' that two given indices indicates.\n  Args.:\n    - arr: Pointer to array to be sorted.\n    - idx1, idx2 : Two indices to be swapped.\n*\/\nstatic void swap(int* arr, int idx1, int idx2)\n{\n    int t = arr[idx1];\n    arr[idx1] = arr[idx2];\n    arr[idx2] = t;\n}\n\n\/*\n  Name: maxHeapify\n  Desc.: Makes given tree and it's sub-trees to be max heap trees.\n  Args:\n    - arr : Point to array to be sorted.\n    - arrayLastIdx : Last index of the given array.\n    - parentIdx : Index of the parent node of the tree.\n*\/\nstatic void maxHeapify(int* arr, int arrayLastIdx, int parentIdx)\n{\n    int lcIdx = (parentIdx * 2) + 1;  \/\/ Index for left child node.\n    int rcIdx = lcIdx + 1;            \/\/ Index for right child node.\n    int biggerChildIdx = lcIdx;       \/\/ Index for child that has bigger value.\n\n    \/\/ Stop recursion if left child index exceeds last index of the array.\n    if(lcIdx > arrayLastIdx)\n        return;\n\n    \/\/ Left child would be the only on if there's no right child. \n    if(rcIdx > arrayLastIdx)\n        biggerChildIdx = lcIdx;\n    else   \/\/ Which of left\/right is bigger if both are exits?\n        biggerChildIdx = (arr[lcIdx] &lt; arr[rcIdx]) ? rcIdx : lcIdx;\n\n    \/\/ Swap and heapify if child is bigger than parent.\n    if(arr[parentIdx] &lt; arr[biggerChildIdx])\n    {\n        swap(arr, parentIdx, biggerChildIdx);\n        maxHeapify(arr, arrayLastIdx, biggerChildIdx);\n    }\n\n    return;\n}\n\n\n\/*\n  Name: buildHeapTree\n  Desc.: Makes sure all sub-trees to be max heapified.\n  Args.:\n    - arr : Pointer to array to be sorted.\n    - arrSize : Size of the array.\n*\/\nstatic void buildHeapTree(int* arr, int arrSize)\n{\n    int parentIdx = (arrSize \/ 2) - 1;\n    int arrayLastIdx = arrSize - 1;\n\n    for(; parentIdx >= 0; parentIdx--)\n    {\n        maxHeapify(arr, arrayLastIdx, parentIdx);\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Heap sort\ub294 root node\uc5d0 \ud56d\uc0c1 \uc81c\uc77c \uc791\uc740 \uac12\uc774\ub098 \uc81c\uc77c \ud070 \uac12\uc774 \uc624\ub3c4\ub85d heap tree\ub97c \uad6c\uc131\ud574\uc11c \uc815\ub82c\ud558\ub294 \ubc29\ubc95\uc774\ub2e4. Heap tree\uc758 root\uc5d0 \ucd5c\uc18c&nbsp;\uac12\uc774 \uc624\ub3c4\ub85d \uad6c\ud604\ud558\uba74 min heap tree, \ucd5c\ub300 \uac12\uc774 \uc624\ub3c4\ub85d \ud558\uba74 max heap tree\ub77c \ud55c\ub2e4. Heap tree\ub97c \uad6c\uc131\ud558\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574\uc11c\ub294 \ubcc4\ub3c4\uc758 \ud3ec\uc2a4\ud2b8\ub85c \ub2e4\ub8e8\uace0 \uc5ec\uae30\uc11c\ub294 max heap\uc73c\ub85c \uc624\ub984\ucc28\uc21c \uc815\ub82c\ud558\ub294 \uacfc\uc815\uc744 \uc124\uba85\ud55c\ub2e4. Heap sort\ub294 \ud55c\ubc88 heap tree\ub97c \uad6c\uc131\ud55c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[10,62,116],"class_list":["post-1587","post","type-post","status-publish","format-standard","hentry","category-programming","tag-algorithm","tag-heap","tag-sort"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/litcoder.com\/index.php?rest_route=\/wp\/v2\/posts\/1587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/litcoder.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/litcoder.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/litcoder.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/litcoder.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1587"}],"version-history":[{"count":31,"href":"https:\/\/litcoder.com\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions"}],"predecessor-version":[{"id":3623,"href":"https:\/\/litcoder.com\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions\/3623"}],"wp:attachment":[{"href":"https:\/\/litcoder.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/litcoder.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/litcoder.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}