TheAlgorithms-C/d5/d4c/group__sorting.html
2020-08-26 16:14:44 +00:00

536 lines
37 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">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.19"/>
<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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">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.8.19 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</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 name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:ga4fdb8af29a07ac8f496e49a11bf9f1bd"><td class="memItemLeft" align="right" valign="top"><a id="ga4fdb8af29a07ac8f496e49a11bf9f1bd"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j)&#160;&#160;&#160;beads[i * <a class="el" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a> + 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 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: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"><a id="gaeccaf61ff47279384d1dba8d869d5c2f"></a>
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. <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 LEN)</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"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga2fb01e00dedb437a42010f6309e7eba8">&#9670;&nbsp;</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"><a name="l00038"></a><span class="lineno"> 38</span>&#160;{</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; <span class="keywordtype">int</span> i, j, <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>, sum;</div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *beads;</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; </div>
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; <span class="keywordflow">for</span> (i = 1, <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a> = a[0]; i &lt; len; i++)</div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; <span class="keywordflow">if</span> (a[i] &gt; <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>)</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a> = a[i];</div>
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; </div>
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160; beads = calloc(1, <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a> * len);</div>
<div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160; </div>
<div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; <span class="comment">/* mark the beads */</span></div>
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; len; i++)</div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; a[i]; j++) <a class="code" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j) = 1;</div>
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; </div>
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>; j++)</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; {</div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="comment">/* count how many beads are on each post */</span></div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; <span class="keywordflow">for</span> (sum = i = 0; i &lt; len; i++)</div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; {</div>
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; sum += <a class="code" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j);</div>
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; <a class="code" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j) = 0;</div>
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160; }</div>
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160; <span class="comment">/* mark bottom sum beads */</span></div>
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160; <span class="keywordflow">for</span> (i = len - sum; i &lt; len; i++) <a class="code" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j) = 1;</div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; }</div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; </div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; len; i++)</div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; {</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a> &amp;&amp; <a class="code" href="../../d5/d4c/group__sorting.html#ga4fdb8af29a07ac8f496e49a11bf9f1bd">BEAD</a>(i, j); j++)</div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; ;</div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; a[i] = j;</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; }</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; free(beads);</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gad7ed8cc4603f500d610054680d28b971"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gad7ed8cc4603f500d610054680d28b971">&#9670;&nbsp;</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"><a name="l00024"></a><span class="lineno"> 24</span>&#160;{</div>
<div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n; i++)</div>
<div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160; {</div>
<div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160; printf(<span class="stringliteral">&quot;%d &quot;</span>, arr[i]);</div>
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160; }</div>
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160; </div>
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160; printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga8dc3ec66cb3350313fdb34bfd1674729"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga8dc3ec66cb3350313fdb34bfd1674729">&#9670;&nbsp;</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"><a name="l00034"></a><span class="lineno"> 34</span>&#160;{</div>
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160; <span class="keywordtype">int</span> *b = (<span class="keywordtype">int</span> *)malloc(n * <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)); <span class="comment">/* dynamic memory must be freed */</span></div>
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; <span class="keywordtype">int</span> c = l;</div>
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160; <span class="keywordtype">int</span> p1, p2;</div>
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160; p1 = l;</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; p2 = ((l + r) / 2) + 1;</div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; <span class="keywordflow">while</span> ((p1 &lt; ((l + r) / 2) + 1) &amp;&amp; (p2 &lt; r + 1))</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; {</div>
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; <span class="keywordflow">if</span> (a[p1] &lt;= a[p2])</div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; {</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; b[c++] = a[p1];</div>
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; p1++;</div>
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160; }</div>
<div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; {</div>
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160; b[c++] = a[p2];</div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; p2++;</div>
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; }</div>
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; }</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; </div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="keywordflow">if</span> (p2 == r + 1)</div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; {</div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; <span class="keywordflow">while</span> ((p1 &lt; ((l + r) / 2) + 1))</div>
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; {</div>
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; b[c++] = a[p1];</div>
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160; p1++;</div>
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160; }</div>
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160; }</div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; {</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; <span class="keywordflow">while</span> ((p2 &lt; r + 1))</div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; {</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; b[c++] = a[p2];</div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; p2++;</div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; }</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; }</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; </div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; <span class="keywordflow">for</span> (c = l; c &lt; r - l + 1; c++) a[c] = b[c];</div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; </div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; free(b);</div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gab99b8a397bdd0bf2903d66c22ba4ba43"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gab99b8a397bdd0bf2903d66c22ba4ba43">&#9670;&nbsp;</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"><a name="l00083"></a><span class="lineno"> 83</span>&#160;{</div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; <span class="keywordflow">if</span> (r - l == 1)</div>
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; {</div>
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; <span class="keywordflow">if</span> (a[l] &gt; a[r])</div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <a class="code" href="../../d5/d4c/group__sorting.html#ga4b9708d87be7a409eff20e5e7e8b43c8">swap</a>(&amp;a[l], &amp;a[r]);</div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160; }</div>
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (l != r)</div>
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160; {</div>
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160; <a class="code" href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">merge_sort</a>(a, n, l, (l + r) / 2);</div>
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160; <a class="code" href="../../d5/d4c/group__sorting.html#gab99b8a397bdd0bf2903d66c22ba4ba43">merge_sort</a>(a, n, ((l + r) / 2) + 1, r);</div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160; <a class="code" href="../../d5/d4c/group__sorting.html#ga8dc3ec66cb3350313fdb34bfd1674729">merge</a>(a, l, r, n);</div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160; }</div>
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160; </div>
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160; <span class="comment">/* no change if l == r */</span></div>
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160;}</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="204" height="88"><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"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga5bc16eaf3ffe6a6ab66780dd445904c0">&#9670;&nbsp;</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"><a name="l00042"></a><span class="lineno"> 42</span>&#160;{</div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; <span class="keyword">const</span> <span class="keywordtype">int</span> gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; <span class="keyword">const</span> <span class="keywordtype">int</span> gap_len = 8;</div>
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; <span class="keywordtype">long</span> i, j, g;</div>
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160; </div>
<div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160; <span class="keywordflow">for</span> (g = 0; g &lt; gap_len; g++)</div>
<div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; { <span class="comment">// for each gap</span></div>
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160; <span class="keywordtype">int</span> gap = gaps[g];</div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; <span class="keywordflow">for</span> (i = gap; i &lt; LEN; i++)</div>
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; { <span class="comment">// from gap position to the end</span></div>
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; <span class="keywordtype">int</span> tmp = array[i];</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; </div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="keywordflow">for</span> (j = i; j &gt;= gap &amp;&amp; (array[j - gap] - tmp) &gt; 0; j -= gap)</div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; array[j] = array[j - gap];</div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; array[j] = tmp;</div>
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; }</div>
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; }</div>
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160;<span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; LEN; i++) printf(<span class="stringliteral">&quot;%s\t&quot;</span>, <a class="code" href="../../df/dea/structdata.html">data</a>[i]);</div>
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga4b9708d87be7a409eff20e5e7e8b43c8"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga4b9708d87be7a409eff20e5e7e8b43c8">&#9670;&nbsp;</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"><a name="l00018"></a><span class="lineno"> 18</span>&#160;{</div>
<div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160; <span class="keywordtype">int</span> t;</div>
<div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160; t = *a;</div>
<div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160; *a = *b;</div>
<div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160; *b = t;</div>
<div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div class="ttc" id="agroup__sorting_html_ga4b9708d87be7a409eff20e5e7e8b43c8"><div class="ttname"><a href="../../d5/d4c/group__sorting.html#ga4b9708d87be7a409eff20e5e7e8b43c8">swap</a></div><div class="ttdeci">void swap(int *a, int *b)</div><div class="ttdoc">Swap two integer variables.</div><div class="ttdef"><b>Definition:</b> merge_sort.c:17</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 class="ttc" id="agroup__sorting_html_ga8dc3ec66cb3350313fdb34bfd1674729"><div class="ttname"><a href="../../d5/d4c/group__sorting.html#ga8dc3ec66cb3350313fdb34bfd1674729">merge</a></div><div class="ttdeci">void merge(int *a, int l, int r, int n)</div><div class="ttdoc">Perform merge of segments.</div><div class="ttdef"><b>Definition:</b> merge_sort.c:33</div></div>
<div class="ttc" id="agroup__kohonen__2d_html_gaffe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a></div><div class="ttdeci">#define max(a, b)</div><div class="ttdoc">shorthand for maximum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:39</div></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 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>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="http://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.19 </li>
</ul>
</div>
</body>
</html>