Include dependency graph for sentinel_linear_search.c:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../dc/dc7/sentinel__linear__search_8c__incl.svg"width="166"height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<trclass="memitem:a16a7a1ef83db52643fe23d48280065ce"><tdclass="memItemLeft"align="right"valign="top">int </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../d5/de2/sentinel__linear__search_8c.html#a16a7a1ef83db52643fe23d48280065ce">sentinel_linear_search</a> (int arr[], int len, int key)</td></tr>
<divclass="textblock"><p><ahref="https://en.wikipedia.org/wiki/Linear_search#With_a_sentinel"target="_blank">Linear Search with Sentinel</a> algorithm implementation </p>
<p>This algorithm saves the last element of the array, then replaces it with the value to be found and sets it as the sentinel. When searching, compares each element with the sentinel. If the same, returns the index. If the index is the index of the sentinel, it means it was not found. Of course, if the value to be found is the last element, we return the index of the last element. </p><dlclass="section author"><dt>Author</dt><dd><ahref="https://github.com/ReganYue"target="_blank">Regan Yue</a> Time Complexity: O(N) </dd></dl>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d5/de2/sentinel__linear__search_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg"width="352"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<p>Utility function to search for an element in the array and return the index of the element</p>
<p>The so-called "sentinel" is to use a special value as the boundary key of the array. One less judgment statement can be used. The purpose is to avoid checking whether the entire array is searched at each step in the search process, so as to improve the efficiency of the program. We can use the last value of the array as the "sentinel", the data storage index i starts from 0 and ends at len-1, then the position where the index of arr is n-1 indicates that there is no data temporarily, which is the "sentinel" key. If the last element of the array is equal to the key, directly return the index of the last element. Before setting the last element of the array as the key, we hand over the last element of the array to temp for temporary storage. Then go to the array to find the key. If the key is found, stop the search, and then compare the found element index with len-1. If it is equal, it means it was not found. If it is not equal, it is found. </p><dlclass="params"><dt>Parameters</dt><dd>
<tableclass="params">
<tr><tdclass="paramname">arr</td><td>this is an array containing elements </td></tr>
<tr><tdclass="paramname">len</td><td>this is the number of elements in the array </td></tr>
<tr><tdclass="paramname">key</td><td>the value we want to search </td></tr>
</table>
</dd>
</dl>
<dlclass="section return"><dt>Returns</dt><dd>i if found, otherwise -1 is returned. </dd></dl>
<divclass="line"><aid="l00065"name="l00065"></a><spanclass="lineno"> 65</span> assert(<aclass="code hl_function"href="../../d5/de2/sentinel__linear__search_8c.html#a16a7a1ef83db52643fe23d48280065ce">sentinel_linear_search</a>( arr, n, 1 )==0);</div>
<divclass="line"><aid="l00066"name="l00066"></a><spanclass="lineno"> 66</span> assert(<aclass="code hl_function"href="../../d5/de2/sentinel__linear__search_8c.html#a16a7a1ef83db52643fe23d48280065ce">sentinel_linear_search</a>( arr, n, 2 )==1);</div>
<divclass="line"><aid="l00067"name="l00067"></a><spanclass="lineno"> 67</span> assert(<aclass="code hl_function"href="../../d5/de2/sentinel__linear__search_8c.html#a16a7a1ef83db52643fe23d48280065ce">sentinel_linear_search</a>( arr, n, 6 )==3);</div>
<divclass="line"><aid="l00068"name="l00068"></a><spanclass="lineno"> 68</span> assert(<aclass="code hl_function"href="../../d5/de2/sentinel__linear__search_8c.html#a16a7a1ef83db52643fe23d48280065ce">sentinel_linear_search</a>( arr, n, 101 )==-1);</div>
<divclass="line"><aid="l00069"name="l00069"></a><spanclass="lineno"> 69</span> printf(<spanclass="stringliteral">"All test cases have successfully passed!\n"</span>);</div>
<divclass="ttc"id="asentinel__linear__search_8c_html_a16a7a1ef83db52643fe23d48280065ce"><divclass="ttname"><ahref="../../d5/de2/sentinel__linear__search_8c.html#a16a7a1ef83db52643fe23d48280065ce">sentinel_linear_search</a></div><divclass="ttdeci">int sentinel_linear_search(int arr[], int len, int key)</div><divclass="ttdoc">for IO operations</div><divclass="ttdef"><b>Definition:</b> sentinel_linear_search.c:36</div></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d5/de2/sentinel__linear__search_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg"width="254"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<liclass="footer">Generated by <ahref="https://www.doxygen.org/index.html"><imgclass="footer"src="../../doxygen.svg"width="104"height="31"alt="doxygen"/></a> 1.9.2 </li>