Files
C-Plus-Plus/d2/d52/heap__sort_8cpp.html
2025-07-09 10:53:16 +00:00

274 lines
17 KiB
HTML

<!-- HTML header for doxygen 1.12.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.14.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: sorting/heap_sort.cpp File Reference</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript">
window.MathJax = {
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/ams']
},
tex: {
macros: {},
packages: ['base','configmacros','ams']
}
};
</script>
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
<td id="projectalign">
<div id="projectname">TheAlgorithms/C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">All the algorithms implemented in C++</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.14.0 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
</script>
<script type="text/javascript">
$(function() { codefold.init(); });
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(function(){initNavTree('d2/d52/heap__sort_8cpp.html','../../',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">heap_sort.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Heapsort" target="_blank">Heap Sort Algorithm (heap sort)</a> implementation
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;algorithm&gt;</code><br />
<code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for heap_sort.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../dc/dfa/heap__sort_8cpp__incl.svg" width="270" height="111"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
<p><a href="../../d2/d52/heap__sort_8cpp_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a9ed3e1510afdf3edd06cf2b68769a767" id="r_a9ed3e1510afdf3edd06cf2b68769a767"><td class="memTemplParams" colspan="2">template&lt;typename T&gt; </td></tr>
<tr class="memitem:a9ed3e1510afdf3edd06cf2b68769a767 template"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a9ed3e1510afdf3edd06cf2b68769a767">printArray</a> (T *arr, int sz)</td></tr>
<tr class="memitem:gabd9a93f241c5c86f2ec0abf8b66b4145" id="r_gabd9a93f241c5c86f2ec0abf8b66b4145"><td class="memTemplParams" colspan="2">template&lt;typename T&gt; </td></tr>
<tr class="memitem:gabd9a93f241c5c86f2ec0abf8b66b4145 template"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#gabd9a93f241c5c86f2ec0abf8b66b4145">heapify</a> (T *arr, int n, int i)</td></tr>
<tr class="memitem:ga29d28b140174dbdde7c9f5157758435f" id="r_ga29d28b140174dbdde7c9f5157758435f"><td class="memTemplParams" colspan="2">template&lt;typename T&gt; </td></tr>
<tr class="memitem:ga29d28b140174dbdde7c9f5157758435f template"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga29d28b140174dbdde7c9f5157758435f">heapSort</a> (T *arr, int n)</td></tr>
<tr class="memitem:ae1a3968e7947464bee7714f6d43b7002" id="r_ae1a3968e7947464bee7714f6d43b7002"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae1a3968e7947464bee7714f6d43b7002">test</a> ()</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
</table>
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/Heapsort" target="_blank">Heap Sort Algorithm (heap sort)</a> implementation </p>
<dl class="section author"><dt>Author</dt><dd><a href="http://github.com/ayaankhan98" target="_blank">Ayaan Khan</a></dd></dl>
<p>Heap-sort is a comparison-based sorting algorithm. Heap-sort can be thought of as an improved selection sort: like selection sort, heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heap sort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step.</p>
<p>Time Complexity - \(O(n \log(n))\) </p>
<p class="definition">Definition in file <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html">heap_sort.cpp</a>.</p>
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function </p>
<p class="definition">Definition at line <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html#l00120">120</a> of file <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html">heap_sort.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 120</span> {</div>
<div class="line"><span class="lineno"> 121</span> <a class="code hl_function" href="#ae1a3968e7947464bee7714f6d43b7002">test</a>();</div>
<div class="line"><span class="lineno"> 122</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 123</span>}</div>
<div class="ttc" id="aheap__sort_8cpp_html_ae1a3968e7947464bee7714f6d43b7002"><div class="ttname"><a href="#ae1a3968e7947464bee7714f6d43b7002">test</a></div><div class="ttdeci">void test()</div><div class="ttdef"><b>Definition</b> <a href="../../d2/d52/heap__sort_8cpp_source.html#l00099">heap_sort.cpp:99</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a9ed3e1510afdf3edd06cf2b68769a767" name="a9ed3e1510afdf3edd06cf2b68769a767"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a9ed3e1510afdf3edd06cf2b68769a767">&#9670;&#160;</a></span>printArray()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename T&gt; </div>
<table class="memname">
<tr>
<td class="memname">void printArray </td>
<td>(</td>
<td class="paramtype">T *</td> <td class="paramname"><span class="paramname"><em>arr</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int</td> <td class="paramname"><span class="paramname"><em>sz</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Utility function to print the array after sorting.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">arr</td><td>array to be printed </td></tr>
<tr><td class="paramname">sz</td><td>size of array </td></tr>
</table>
</dd>
</dl>
<p class="definition">Definition at line <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html#l00037">37</a> of file <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html">heap_sort.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 37</span> {</div>
<div class="line"><span class="lineno"> 38</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; sz; i++) std::cout &lt;&lt; arr[i] &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div>
<div class="line"><span class="lineno"> 39</span> std::cout &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 40</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ae1a3968e7947464bee7714f6d43b7002" name="ae1a3968e7947464bee7714f6d43b7002"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae1a3968e7947464bee7714f6d43b7002">&#9670;&#160;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Test cases to test the program </p>
<p class="definition">Definition at line <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html#l00099">99</a> of file <a class="el" href="../../d2/d52/heap__sort_8cpp_source.html">heap_sort.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 99</span> {</div>
<div class="line"><span class="lineno"> 100</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Test 1\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 101</span> <span class="keywordtype">int</span> arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0};</div>
<div class="line"><span class="lineno"> 102</span> <span class="keywordtype">int</span> sz = <span class="keyword">sizeof</span>(arr) / <span class="keyword">sizeof</span>(arr[0]); <span class="comment">// sz - size of array</span></div>
<div class="line"><span class="lineno"> 103</span> <a class="code hl_function" href="#a9ed3e1510afdf3edd06cf2b68769a767">printArray</a>(arr, sz); <span class="comment">// displaying the array before sorting</span></div>
<div class="line"><span class="lineno"> 104</span> <a class="code hl_function" href="../../d5/d4c/group__sorting.html#ga29d28b140174dbdde7c9f5157758435f">heapSort</a>(arr, sz); <span class="comment">// calling heapsort to sort the array</span></div>
<div class="line"><span class="lineno"> 105</span> <a class="code hl_function" href="#a9ed3e1510afdf3edd06cf2b68769a767">printArray</a>(arr, sz); <span class="comment">// display array after sorting</span></div>
<div class="line"><span class="lineno"> 106</span> assert(std::is_sorted(arr, arr + sz));</div>
<div class="line"><span class="lineno"> 107</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Test 1 Passed\n========================\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 108</span> </div>
<div class="line"><span class="lineno"> 109</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Test 2\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 110</span> <span class="keywordtype">double</span> arr2[] = {4.5, -3.6, 7.6, 0, 12.9};</div>
<div class="line"><span class="lineno"> 111</span> sz = <span class="keyword">sizeof</span>(arr2) / <span class="keyword">sizeof</span>(arr2[0]);</div>
<div class="line"><span class="lineno"> 112</span> <a class="code hl_function" href="#a9ed3e1510afdf3edd06cf2b68769a767">printArray</a>(arr2, sz);</div>
<div class="line"><span class="lineno"> 113</span> <a class="code hl_function" href="../../d5/d4c/group__sorting.html#ga29d28b140174dbdde7c9f5157758435f">heapSort</a>(arr2, sz);</div>
<div class="line"><span class="lineno"> 114</span> <a class="code hl_function" href="#a9ed3e1510afdf3edd06cf2b68769a767">printArray</a>(arr2, sz);</div>
<div class="line"><span class="lineno"> 115</span> assert(std::is_sorted(arr2, arr2 + sz));</div>
<div class="line"><span class="lineno"> 116</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Test 2 passed\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 117</span>}</div>
<div class="ttc" id="agroup__sorting_html_ga29d28b140174dbdde7c9f5157758435f"><div class="ttname"><a href="../../d5/d4c/group__sorting.html#ga29d28b140174dbdde7c9f5157758435f">heapSort</a></div><div class="ttdeci">void heapSort(T *arr, int n)</div><div class="ttdef"><b>Definition</b> <a href="../../d2/d52/heap__sort_8cpp_source.html#l00084">heap_sort.cpp:84</a></div></div>
<div class="ttc" id="aheap__sort_8cpp_html_a9ed3e1510afdf3edd06cf2b68769a767"><div class="ttname"><a href="#a9ed3e1510afdf3edd06cf2b68769a767">printArray</a></div><div class="ttdeci">void printArray(T *arr, int sz)</div><div class="ttdef"><b>Definition</b> <a href="../../d2/d52/heap__sort_8cpp_source.html#l00037">heap_sort.cpp:37</a></div></div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div id="page-nav" class="page-nav-panel">
<div id="page-nav-resize-handle"></div>
<div id="page-nav-tree">
<div id="page-nav-contents">
</div><!-- page-nav-contents -->
</div><!-- page-nav-tree -->
</div><!-- page-nav -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a href="../../dir_bb1b521853a9c46347182a9d10420771.html">sorting</a></li><li class="navelem"><a href="../../d2/d52/heap__sort_8cpp.html">heap_sort.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
</ul>
</div>
</body>
</html>