TheAlgorithms-C/d3/d47/modified__binary__search_8c.html
2021-02-05 19:52:57 +00:00

324 lines
22 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.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: searching/modified_binary_search.c File Reference</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.9.1 -->
<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','.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: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('d3/d47/modified__binary__search_8c.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="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">modified_binary_search.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p><a href="https://arxiv.org/abs/1406.1677">Modified binary search algorithm</a>
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for modified_binary_search.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d4/dee/modified__binary__search_8c__incl.svg" width="160" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a0cfaa56aacf960e4628a0fb27add8890"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a> (const int **mat, int i, int j_low, int j_high, int x)</td></tr>
<tr class="memdesc:a0cfaa56aacf960e4628a0fb27add8890"><td class="mdescLeft">&#160;</td><td class="mdescRight">This function does Binary search for <code>x</code> in <code>i</code>-th row from <code>j_low</code> to <code>j_high</code>. <a href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">More...</a><br /></td></tr>
<tr class="separator:a0cfaa56aacf960e4628a0fb27add8890"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a7df9a198e30cded6229d79bef7591f8f"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d47/modified__binary__search_8c.html#a7df9a198e30cded6229d79bef7591f8f">modifiedBinarySearch</a> (const int **mat, int n, int m, int x)</td></tr>
<tr class="memdesc:a7df9a198e30cded6229d79bef7591f8f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found. <a href="../../d3/d47/modified__binary__search_8c.html#a7df9a198e30cded6229d79bef7591f8f">More...</a><br /></td></tr>
<tr class="separator:a7df9a198e30cded6229d79bef7591f8f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top"><a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d47/modified__binary__search_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p><a href="https://arxiv.org/abs/1406.1677">Modified binary search algorithm</a> </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a0cfaa56aacf960e4628a0fb27add8890"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0cfaa56aacf960e4628a0fb27add8890">&#9670;&nbsp;</a></span>binarySearch()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int binarySearch </td>
<td>(</td>
<td class="paramtype">const int **&#160;</td>
<td class="paramname"><em>mat</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>i</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>j_low</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>j_high</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>This function does Binary search for <code>x</code> in <code>i</code>-th row from <code>j_low</code> to <code>j_high</code>. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">mat</td><td>2D matrix to search within </td></tr>
<tr><td class="paramname">i</td><td>row to search in </td></tr>
<tr><td class="paramname">j_low</td><td>start column index </td></tr>
<tr><td class="paramname">j_high</td><td>end column index </td></tr>
<tr><td class="paramname">x</td><td>value to search for </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>column where <code>x</code> was found </dd>
<dd>
-1 if value not found </dd></dl>
<div class="fragment"><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;{</div>
<div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160; <span class="keywordflow">while</span> (j_low &lt;= j_high)</div>
<div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160; {</div>
<div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160; <span class="keywordtype">int</span> j_mid = (j_low + j_high) / 2;</div>
<div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160; </div>
<div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160; <span class="comment">// Element found</span></div>
<div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160; <span class="keywordflow">if</span> (mat[i][j_mid] == x)</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;Found at (%d,%d)\n&quot;</span>, i, j_mid);</div>
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160; <span class="keywordflow">return</span> j_mid;</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; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (mat[i][j_mid] &gt; x)</div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160; j_high = j_mid - 1;</div>
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160; j_low = j_mid + 1;</div>
<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; </div>
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; <span class="comment">// element not found</span></div>
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160; printf(<span class="stringliteral">&quot;element not found\n&quot;</span>);</div>
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160; <span class="keywordflow">return</span> -1;</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a7df9a198e30cded6229d79bef7591f8f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7df9a198e30cded6229d79bef7591f8f">&#9670;&nbsp;</a></span>modifiedBinarySearch()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void modifiedBinarySearch </td>
<td>(</td>
<td class="paramtype">const int **&#160;</td>
<td class="paramname"><em>mat</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>m</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">mat</td><td>matrix to search for the value in </td></tr>
<tr><td class="paramdir"></td><td class="paramname">n</td><td>number of rows in the matrix </td></tr>
<tr><td class="paramdir"></td><td class="paramname">m</td><td>number of columns in the matrix </td></tr>
<tr><td class="paramdir"></td><td class="paramname">x</td><td>value to search for </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160;{ <span class="comment">// If Single row matrix</span></div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; <span class="keywordflow">if</span> (n == 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; <a class="code" href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a>(mat, 0, 0, m - 1, x);</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; }</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="comment">// Do binary search in middle column.</span></div>
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; <span class="comment">// Condition to terminate the loop when the 2 desired rows are found.</span></div>
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; <span class="keywordtype">int</span> i_low = 0, i_high = n - 1, j_mid = m / 2;</div>
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160; <span class="keywordflow">while</span> ((i_low + 1) &lt; i_high)</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; <span class="keywordtype">int</span> i_mid = (i_low + i_high) / 2;</div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; <span class="comment">// element found</span></div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <span class="keywordflow">if</span> (mat[i_mid][j_mid] == x)</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; {</div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; printf(<span class="stringliteral">&quot;Found at (%d,%d)\n&quot;</span>, i_mid, j_mid);</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; <span class="keywordflow">return</span>;</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; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (mat[i_mid][j_mid] &gt; x)</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; i_high = i_mid;</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; i_low = i_mid;</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; <span class="comment">// If element is present on the mid of the two rows</span></div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="keywordflow">if</span> (mat[i_low][j_mid] == x)</div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; printf(<span class="stringliteral">&quot;Found at (%d,%d)\n&quot;</span>, i_low, j_mid);</div>
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (mat[i_low + 1][j_mid] == x)</div>
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; printf(<span class="stringliteral">&quot;Found at (%d,%d)\n&quot;</span>, i_low + 1, j_mid);</div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; </div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <span class="comment">// Search element on 1st half of 1st row</span></div>
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (x &lt;= mat[i_low][j_mid - 1])</div>
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160; <a class="code" href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a>(mat, i_low, 0, j_mid - 1, x);</div>
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; </div>
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="comment">// Search element on 2nd half of 1st row</span></div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (x &gt;= mat[i_low][j_mid + 1] &amp;&amp; x &lt;= mat[i_low][m - 1])</div>
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; <a class="code" href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a>(mat, i_low, j_mid + 1, m - 1, x);</div>
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; </div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <span class="comment">// Search element on 1st half of 2nd row</span></div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (x &lt;= mat[i_low + 1][j_mid - 1])</div>
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160; <a class="code" href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a>(mat, i_low + 1, 0, j_mid - 1, x);</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; <span class="comment">// search element on 2nd half of 2nd row</span></div>
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160; <a class="code" href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a>(mat, i_low + 1, j_mid + 1, m - 1, x);</div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160;}</div>
<div class="ttc" id="amodified__binary__search_8c_html_a0cfaa56aacf960e4628a0fb27add8890"><div class="ttname"><a href="../../d3/d47/modified__binary__search_8c.html#a0cfaa56aacf960e4628a0fb27add8890">binarySearch</a></div><div class="ttdeci">int binarySearch(const int **mat, int i, int j_low, int j_high, int x)</div><div class="ttdoc">This function does Binary search for x in i-th row from j_low to j_high.</div><div class="ttdef"><b>Definition:</b> modified_binary_search.c:18</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="../../d3/d47/modified__binary__search_8c_a7df9a198e30cded6229d79bef7591f8f_cgraph.svg" width="307" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</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="navelem"><a class="el" href="../../dir_da574daf9ce9c05b45556bdc871ed1e3.html">searching</a></li><li class="navelem"><a class="el" href="../../d3/d47/modified__binary__search_8c.html">modified_binary_search.c</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.9.1 </li>
</ul>
</div>
</body>
</html>