TheAlgorithms-C/d5/d4c/group__sorting.html
2022-11-25 05:17:20 +00:00

642 lines
40 KiB
HTML

<!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.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: Sorting algorithms</title>
<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>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.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/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.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="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</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">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d5/d4c/group__sorting.html','../../'); initResizable(); });
/* @license-end */
</script>
<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="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">Sorting algorithms</div></div>
</div><!--header-->
<div class="contents">
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:ga4fdb8af29a07ac8f496e49a11bf9f1bd"><td class="memItemLeft" align="right" valign="top"><a id="ga4fdb8af29a07ac8f496e49a11bf9f1bd" name="ga4fdb8af29a07ac8f496e49a11bf9f1bd"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>BEAD</b>(i, j)&#160;&#160;&#160;beads[i * max + j]</td></tr>
<tr class="memdesc:ga4fdb8af29a07ac8f496e49a11bf9f1bd"><td class="mdescLeft">&#160;</td><td class="mdescRight">Create easy access of elements from a 2D matrix stored in memory as a 1D array. <br /></td></tr>
<tr class="separator:ga4fdb8af29a07ac8f496e49a11bf9f1bd"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:gad7ed8cc4603f500d610054680d28b971"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#gad7ed8cc4603f500d610054680d28b971">display</a> (const int *arr, int n)</td></tr>
<tr class="memdesc:gad7ed8cc4603f500d610054680d28b971"><td class="mdescLeft">&#160;</td><td class="mdescRight">Displays the array, passed to this method. <a href="../../d5/d4c/group__sorting.html#gad7ed8cc4603f500d610054680d28b971">More...</a><br /></td></tr>
<tr class="separator:gad7ed8cc4603f500d610054680d28b971"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga2fb01e00dedb437a42010f6309e7eba8"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga2fb01e00dedb437a42010f6309e7eba8">bead_sort</a> (int *a, size_t len)</td></tr>
<tr class="memdesc:ga2fb01e00dedb437a42010f6309e7eba8"><td class="mdescLeft">&#160;</td><td class="mdescRight">This is where the sorting of the array takes place. <a href="../../d5/d4c/group__sorting.html#ga2fb01e00dedb437a42010f6309e7eba8">More...</a><br /></td></tr>
<tr class="separator:ga2fb01e00dedb437a42010f6309e7eba8"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga98666b73777e308fb06a3c489ce25359"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga98666b73777e308fb06a3c489ce25359">RecursionInsertionSort</a> (int *arr, int size)</td></tr>
<tr class="memdesc:ga98666b73777e308fb06a3c489ce25359"><td class="mdescLeft">&#160;</td><td class="mdescRight">Insertion sort algorithm implements using Recursion. <a href="../../d5/d4c/group__sorting.html#ga98666b73777e308fb06a3c489ce25359">More...</a><br /></td></tr>
<tr class="separator:ga98666b73777e308fb06a3c489ce25359"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga4b9708d87be7a409eff20e5e7e8b43c8"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga4b9708d87be7a409eff20e5e7e8b43c8">swap</a> (int *a, int *b)</td></tr>
<tr class="memdesc:ga4b9708d87be7a409eff20e5e7e8b43c8"><td class="mdescLeft">&#160;</td><td class="mdescRight">Swap two integer variables. <a href="../../d5/d4c/group__sorting.html#ga4b9708d87be7a409eff20e5e7e8b43c8">More...</a><br /></td></tr>
<tr class="separator:ga4b9708d87be7a409eff20e5e7e8b43c8"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga8dc3ec66cb3350313fdb34bfd1674729"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga8dc3ec66cb3350313fdb34bfd1674729">merge</a> (int *a, int l, int r, int n)</td></tr>
<tr class="memdesc:ga8dc3ec66cb3350313fdb34bfd1674729"><td class="mdescLeft">&#160;</td><td class="mdescRight">Perform merge of segments. <a href="../../d5/d4c/group__sorting.html#ga8dc3ec66cb3350313fdb34bfd1674729">More...</a><br /></td></tr>
<tr class="separator:ga8dc3ec66cb3350313fdb34bfd1674729"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gab99b8a397bdd0bf2903d66c22ba4ba43"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">merge_sort</a> (int *a, int n, int l, int r)</td></tr>
<tr class="memdesc:gab99b8a397bdd0bf2903d66c22ba4ba43"><td class="mdescLeft">&#160;</td><td class="mdescRight">Merge sort algorithm implementation. <a href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">More...</a><br /></td></tr>
<tr class="separator:gab99b8a397bdd0bf2903d66c22ba4ba43"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gaeccaf61ff47279384d1dba8d869d5c2f"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#gaeccaf61ff47279384d1dba8d869d5c2f">show_data</a> (int *arr, long len)</td></tr>
<tr class="memdesc:gaeccaf61ff47279384d1dba8d869d5c2f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Helper function to print array values. <a href="../../d5/d4c/group__sorting.html#gaeccaf61ff47279384d1dba8d869d5c2f">More...</a><br /></td></tr>
<tr class="separator:gaeccaf61ff47279384d1dba8d869d5c2f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga5bc16eaf3ffe6a6ab66780dd445904c0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga5bc16eaf3ffe6a6ab66780dd445904c0">shell_sort</a> (int *array, long <a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>)</td></tr>
<tr class="memdesc:ga5bc16eaf3ffe6a6ab66780dd445904c0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Shell sort algorithm. <a href="../../d5/d4c/group__sorting.html#ga5bc16eaf3ffe6a6ab66780dd445904c0">More...</a><br /></td></tr>
<tr class="separator:ga5bc16eaf3ffe6a6ab66780dd445904c0"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<h2 class="groupheader">Function Documentation</h2>
<a id="ga2fb01e00dedb437a42010f6309e7eba8" name="ga2fb01e00dedb437a42010f6309e7eba8"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga2fb01e00dedb437a42010f6309e7eba8">&#9670;&#160;</a></span>bead_sort()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void bead_sort </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>len</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>This is where the sorting of the array takes place. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">a</td><td>array to be sorted </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">len</td><td>Array Size </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 38</span>{</div>
<div class="line"><span class="lineno"> 39</span> <span class="keywordtype">int</span> i, j, max, sum;</div>
<div class="line"><span class="lineno"> 40</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *beads;</div>
<div class="line"><span class="lineno"> 41</span> </div>
<div class="line"><span class="lineno"> 42</span> <span class="keywordflow">for</span> (i = 1, max = a[0]; i &lt; len; i++)</div>
<div class="line"><span class="lineno"> 43</span> <span class="keywordflow">if</span> (a[i] &gt; max)</div>
<div class="line"><span class="lineno"> 44</span> max = a[i];</div>
<div class="line"><span class="lineno"> 45</span> </div>
<div class="line"><span class="lineno"> 46</span> beads = <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(1, max * len);</div>
<div class="line"><span class="lineno"> 47</span> </div>
<div class="line"><span class="lineno"> 48</span> <span class="comment">/* mark the beads */</span></div>
<div class="line"><span class="lineno"> 49</span> <span class="keywordflow">for</span> (i = 0; i &lt; len; i++)</div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordflow">for</span> (j = 0; j &lt; a[i]; j++) <a class="code hl_define" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j) = 1;</div>
<div class="line"><span class="lineno"> 51</span> </div>
<div class="line"><span class="lineno"> 52</span> <span class="keywordflow">for</span> (j = 0; j &lt; max; j++)</div>
<div class="line"><span class="lineno"> 53</span> {</div>
<div class="line"><span class="lineno"> 54</span> <span class="comment">/* count how many beads are on each post */</span></div>
<div class="line"><span class="lineno"> 55</span> <span class="keywordflow">for</span> (sum = i = 0; i &lt; len; i++)</div>
<div class="line"><span class="lineno"> 56</span> {</div>
<div class="line"><span class="lineno"> 57</span> sum += <a class="code hl_define" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j);</div>
<div class="line"><span class="lineno"> 58</span> <a class="code hl_define" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j) = 0;</div>
<div class="line"><span class="lineno"> 59</span> }</div>
<div class="line"><span class="lineno"> 60</span> <span class="comment">/* mark bottom sum beads */</span></div>
<div class="line"><span class="lineno"> 61</span> <span class="keywordflow">for</span> (i = len - sum; i &lt; len; i++) <a class="code hl_define" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j) = 1;</div>
<div class="line"><span class="lineno"> 62</span> }</div>
<div class="line"><span class="lineno"> 63</span> </div>
<div class="line"><span class="lineno"> 64</span> <span class="keywordflow">for</span> (i = 0; i &lt; len; i++)</div>
<div class="line"><span class="lineno"> 65</span> {</div>
<div class="line"><span class="lineno"> 66</span> <span class="keywordflow">for</span> (j = 0; j &lt; max &amp;&amp; <a class="code hl_define" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j); j++)</div>
<div class="line"><span class="lineno"> 67</span> ;</div>
<div class="line"><span class="lineno"> 68</span> a[i] = j;</div>
<div class="line"><span class="lineno"> 69</span> }</div>
<div class="line"><span class="lineno"> 70</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(beads);</div>
<div class="line"><span class="lineno"> 71</span>}</div>
<div class="ttc" id="agroup__sorting_html_ga4fdb8af29a07ac8f496e49a11bf9f1bd"><div class="ttname"><a href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a></div><div class="ttdeci">#define BEAD(i, j)</div><div class="ttdoc">Create easy access of elements from a 2D matrix stored in memory as a 1D array.</div><div class="ttdef"><b>Definition:</b> bead_sort.c:16</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_afdddaa949a93c1ef559a638e98f9c21b"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a></div><div class="ttdeci">#define calloc(elemCount, elemSize)</div><div class="ttdoc">This macro replace the standard calloc function with calloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:22</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="gad7ed8cc4603f500d610054680d28b971" name="gad7ed8cc4603f500d610054680d28b971"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gad7ed8cc4603f500d610054680d28b971">&#9670;&#160;</a></span>display()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void display </td>
<td>(</td>
<td class="paramtype">const int *&#160;</td>
<td class="paramname"><em>arr</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>n</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Displays the array, passed to this method. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">arr</td><td>array to display </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">n</td><td>number of elements in the array </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 24</span>{</div>
<div class="line"><span class="lineno"> 25</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n; i++)</div>
<div class="line"><span class="lineno"> 26</span> {</div>
<div class="line"><span class="lineno"> 27</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, arr[i]);</div>
<div class="line"><span class="lineno"> 28</span> }</div>
<div class="line"><span class="lineno"> 29</span> </div>
<div class="line"><span class="lineno"> 30</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 31</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga8dc3ec66cb3350313fdb34bfd1674729" name="ga8dc3ec66cb3350313fdb34bfd1674729"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga8dc3ec66cb3350313fdb34bfd1674729">&#9670;&#160;</a></span>merge()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void merge </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>l</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>r</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>n</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Perform merge of segments. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>array to sort </td></tr>
<tr><td class="paramname">l</td><td>left index for merge </td></tr>
<tr><td class="paramname">r</td><td>right index for merge </td></tr>
<tr><td class="paramname">n</td><td>total number of elements in the array </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 34</span>{</div>
<div class="line"><span class="lineno"> 35</span> <span class="keywordtype">int</span> *b = (<span class="keywordtype">int</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(n * <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)); <span class="comment">/* dynamic memory must be freed */</span></div>
<div class="line"><span class="lineno"> 36</span> <span class="keywordtype">int</span> c = l;</div>
<div class="line"><span class="lineno"> 37</span> <span class="keywordtype">int</span> p1, p2;</div>
<div class="line"><span class="lineno"> 38</span> p1 = l;</div>
<div class="line"><span class="lineno"> 39</span> p2 = ((l + r) / 2) + 1;</div>
<div class="line"><span class="lineno"> 40</span> <span class="keywordflow">while</span> ((p1 &lt; ((l + r) / 2) + 1) &amp;&amp; (p2 &lt; r + 1))</div>
<div class="line"><span class="lineno"> 41</span> {</div>
<div class="line"><span class="lineno"> 42</span> <span class="keywordflow">if</span> (a[p1] &lt;= a[p2])</div>
<div class="line"><span class="lineno"> 43</span> {</div>
<div class="line"><span class="lineno"> 44</span> b[c++] = a[p1];</div>
<div class="line"><span class="lineno"> 45</span> p1++;</div>
<div class="line"><span class="lineno"> 46</span> }</div>
<div class="line"><span class="lineno"> 47</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 48</span> {</div>
<div class="line"><span class="lineno"> 49</span> b[c++] = a[p2];</div>
<div class="line"><span class="lineno"> 50</span> p2++;</div>
<div class="line"><span class="lineno"> 51</span> }</div>
<div class="line"><span class="lineno"> 52</span> }</div>
<div class="line"><span class="lineno"> 53</span> </div>
<div class="line"><span class="lineno"> 54</span> <span class="keywordflow">if</span> (p2 == r + 1)</div>
<div class="line"><span class="lineno"> 55</span> {</div>
<div class="line"><span class="lineno"> 56</span> <span class="keywordflow">while</span> ((p1 &lt; ((l + r) / 2) + 1))</div>
<div class="line"><span class="lineno"> 57</span> {</div>
<div class="line"><span class="lineno"> 58</span> b[c++] = a[p1];</div>
<div class="line"><span class="lineno"> 59</span> p1++;</div>
<div class="line"><span class="lineno"> 60</span> }</div>
<div class="line"><span class="lineno"> 61</span> }</div>
<div class="line"><span class="lineno"> 62</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 63</span> {</div>
<div class="line"><span class="lineno"> 64</span> <span class="keywordflow">while</span> ((p2 &lt; r + 1))</div>
<div class="line"><span class="lineno"> 65</span> {</div>
<div class="line"><span class="lineno"> 66</span> b[c++] = a[p2];</div>
<div class="line"><span class="lineno"> 67</span> p2++;</div>
<div class="line"><span class="lineno"> 68</span> }</div>
<div class="line"><span class="lineno"> 69</span> }</div>
<div class="line"><span class="lineno"> 70</span> </div>
<div class="line"><span class="lineno"> 71</span> <span class="keywordflow">for</span> (c = l; c &lt; r + 1; c++) a[c] = b[c];</div>
<div class="line"><span class="lineno"> 72</span> </div>
<div class="line"><span class="lineno"> 73</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(b);</div>
<div class="line"><span class="lineno"> 74</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="gab99b8a397bdd0bf2903d66c22ba4ba43" name="gab99b8a397bdd0bf2903d66c22ba4ba43"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gab99b8a397bdd0bf2903d66c22ba4ba43">&#9670;&#160;</a></span>merge_sort()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void merge_sort </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>n</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>l</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>r</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Merge sort algorithm implementation. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>array to sort </td></tr>
<tr><td class="paramname">n</td><td>number of elements in the array </td></tr>
<tr><td class="paramname">l</td><td>index to sort from </td></tr>
<tr><td class="paramname">r</td><td>index to sort till </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 83</span>{</div>
<div class="line"><span class="lineno"> 84</span> <span class="keywordflow">if</span> (r - l == 1)</div>
<div class="line"><span class="lineno"> 85</span> {</div>
<div class="line"><span class="lineno"> 86</span> <span class="keywordflow">if</span> (a[l] &gt; a[r])</div>
<div class="line"><span class="lineno"> 87</span> swap(&amp;a[l], &amp;a[r]);</div>
<div class="line"><span class="lineno"> 88</span> }</div>
<div class="line"><span class="lineno"> 89</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (l != r)</div>
<div class="line"><span class="lineno"> 90</span> {</div>
<div class="line"><span class="lineno"> 91</span> <a class="code hl_function" href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">merge_sort</a>(a, n, l, (l + r) / 2);</div>
<div class="line"><span class="lineno"> 92</span> <a class="code hl_function" href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">merge_sort</a>(a, n, ((l + r) / 2) + 1, r);</div>
<div class="line"><span class="lineno"> 93</span> merge(a, l, r, n);</div>
<div class="line"><span class="lineno"> 94</span> }</div>
<div class="line"><span class="lineno"> 95</span> </div>
<div class="line"><span class="lineno"> 96</span> <span class="comment">/* no change if l == r */</span></div>
<div class="line"><span class="lineno"> 97</span>}</div>
<div class="ttc" id="agroup__sorting_html_gab99b8a397bdd0bf2903d66c22ba4ba43"><div class="ttname"><a href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">merge_sort</a></div><div class="ttdeci">void merge_sort(int *a, int n, int l, int r)</div><div class="ttdoc">Merge sort algorithm implementation.</div><div class="ttdef"><b>Definition:</b> merge_sort.c:82</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d5/d4c/group__sorting_gab99b8a397bdd0bf2903d66c22ba4ba43_cgraph.svg" width="98" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ga98666b73777e308fb06a3c489ce25359" name="ga98666b73777e308fb06a3c489ce25359"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga98666b73777e308fb06a3c489ce25359">&#9670;&#160;</a></span>RecursionInsertionSort()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void RecursionInsertionSort </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>arr</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>size</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Insertion sort algorithm implements using Recursion. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">arr</td><td>array to be sorted </td></tr>
<tr><td class="paramname">size</td><td>size of array </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 21</span>{</div>
<div class="line"><span class="lineno"> 22</span> <span class="keywordflow">if</span>(size &lt;= 0)</div>
<div class="line"><span class="lineno"> 23</span> {</div>
<div class="line"><span class="lineno"> 24</span> <span class="keywordflow">return</span>;</div>
<div class="line"><span class="lineno"> 25</span> }</div>
<div class="line"><span class="lineno"> 26</span> </div>
<div class="line"><span class="lineno"> 27</span> <span class="comment">// marking recursive call to reach starting element</span></div>
<div class="line"><span class="lineno"> 28</span> <a class="code hl_function" href="../../d5/d4c/group__sorting.html#ga98666b73777e308fb06a3c489ce25359">RecursionInsertionSort</a>(arr,size-1);</div>
<div class="line"><span class="lineno"> 29</span> </div>
<div class="line"><span class="lineno"> 30</span> <span class="keywordtype">int</span> key = arr[size-1];</div>
<div class="line"><span class="lineno"> 31</span> <span class="keywordtype">int</span> j = size-2;</div>
<div class="line"><span class="lineno"> 32</span> <span class="comment">// swapping logic for insertion sort</span></div>
<div class="line"><span class="lineno"> 33</span> <span class="keywordflow">while</span>(j &gt;= 0 &amp;&amp; arr[j] &gt; key)</div>
<div class="line"><span class="lineno"> 34</span> {</div>
<div class="line"><span class="lineno"> 35</span> arr[j+1] = arr[j];</div>
<div class="line"><span class="lineno"> 36</span> j--;</div>
<div class="line"><span class="lineno"> 37</span> }</div>
<div class="line"><span class="lineno"> 38</span> arr[j+1] = key;</div>
<div class="line"><span class="lineno"> 39</span>}</div>
<div class="ttc" id="agroup__sorting_html_ga98666b73777e308fb06a3c489ce25359"><div class="ttname"><a href="../../d5/d4c/group__sorting.html#ga98666b73777e308fb06a3c489ce25359">RecursionInsertionSort</a></div><div class="ttdeci">void RecursionInsertionSort(int *arr, int size)</div><div class="ttdoc">Insertion sort algorithm implements using Recursion.</div><div class="ttdef"><b>Definition:</b> insertion_sort_recursive.c:20</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d5/d4c/group__sorting_ga98666b73777e308fb06a3c489ce25359_cgraph.svg" width="166" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ga5bc16eaf3ffe6a6ab66780dd445904c0" name="ga5bc16eaf3ffe6a6ab66780dd445904c0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga5bc16eaf3ffe6a6ab66780dd445904c0">&#9670;&#160;</a></span>shell_sort()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void shell_sort </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>array</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">long&#160;</td>
<td class="paramname"><em>LEN</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Shell sort algorithm. </p>
<p ><br />
Optimized algorithm - takes half the time as other </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">array</td><td>array to sort </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">LEN</td><td>length of the array </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 42</span>{</div>
<div class="line"><span class="lineno"> 43</span> <span class="keyword">const</span> <span class="keywordtype">int</span> gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};</div>
<div class="line"><span class="lineno"> 44</span> <span class="keyword">const</span> <span class="keywordtype">int</span> gap_len = 8;</div>
<div class="line"><span class="lineno"> 45</span> <span class="keywordtype">long</span> i, j, g;</div>
<div class="line"><span class="lineno"> 46</span> </div>
<div class="line"><span class="lineno"> 47</span> <span class="keywordflow">for</span> (g = 0; g &lt; gap_len; g++)</div>
<div class="line"><span class="lineno"> 48</span> { <span class="comment">// for each gap</span></div>
<div class="line"><span class="lineno"> 49</span> <span class="keywordtype">int</span> gap = gaps[g];</div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordflow">for</span> (i = gap; i &lt; <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++)</div>
<div class="line"><span class="lineno"> 51</span> { <span class="comment">// from gap position to the end</span></div>
<div class="line"><span class="lineno"> 52</span> <span class="keywordtype">int</span> tmp = array[i];</div>
<div class="line"><span class="lineno"> 53</span> </div>
<div class="line"><span class="lineno"> 54</span> <span class="keywordflow">for</span> (j = i; j &gt;= gap &amp;&amp; (array[j - gap] - tmp) &gt; 0; j -= gap)</div>
<div class="line"><span class="lineno"> 55</span> array[j] = array[j - gap];</div>
<div class="line"><span class="lineno"> 56</span> array[j] = tmp;</div>
<div class="line"><span class="lineno"> 57</span> }</div>
<div class="line"><span class="lineno"> 58</span> }</div>
<div class="line"><span class="lineno"> 59</span><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordflow">for</span> (i = 0; i &lt; <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++) printf(<span class="stringliteral">&quot;%s\t&quot;</span>, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i]);</div>
<div class="line"><span class="lineno"> 61</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 62</span>}</div>
<div class="ttc" id="aalaw_8c_html_a05b49c662c073f89e86804f7856622a0"><div class="ttname"><a href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a></div><div class="ttdeci">#define LEN</div><div class="ttdoc">Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...</div><div class="ttdef"><b>Definition:</b> alaw.c:28</div></div>
<div class="ttc" id="astructdata_html"><div class="ttname"><a href="../../df/dea/structdata.html">data</a></div><div class="ttdef"><b>Definition:</b> prime_factoriziation.c:25</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="gaeccaf61ff47279384d1dba8d869d5c2f" name="gaeccaf61ff47279384d1dba8d869d5c2f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gaeccaf61ff47279384d1dba8d869d5c2f">&#9670;&#160;</a></span>show_data()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void show_data </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>arr</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">long&#160;</td>
<td class="paramname"><em>len</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Helper function to print array values. </p>
<div class="fragment"><div class="line"><span class="lineno"> 17</span>{</div>
<div class="line"><span class="lineno"> 18</span> <span class="keywordflow">for</span> (<span class="keywordtype">long</span> i = 0; i &lt; len; i++) printf(<span class="stringliteral">&quot;%3d &quot;</span>, arr[i]);</div>
<div class="line"><span class="lineno"> 19</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 20</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga4b9708d87be7a409eff20e5e7e8b43c8" name="ga4b9708d87be7a409eff20e5e7e8b43c8"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga4b9708d87be7a409eff20e5e7e8b43c8">&#9670;&#160;</a></span>swap()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void swap </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>b</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Swap two integer variables. </p>
<p >Function to swap values of two integers.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">a</td><td>pointer to first variable </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">b</td><td>pointer to second variable</td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">a</td><td>reference to first variable </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">b</td><td>reference to second variable </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 18</span>{</div>
<div class="line"><span class="lineno"> 19</span> <span class="keywordtype">int</span> t;</div>
<div class="line"><span class="lineno"> 20</span> t = *a;</div>
<div class="line"><span class="lineno"> 21</span> *a = *b;</div>
<div class="line"><span class="lineno"> 22</span> *b = t;</div>
<div class="line"><span class="lineno"> 23</span>}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<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.9.5 </li>
</ul>
</div>
</body>
</html>