mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-21 12:23:13 +08:00
369 lines
25 KiB
HTML
369 lines
25 KiB
HTML
<!-- HTML header for doxygen 1.12.0-->
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.14.0"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>TheAlgorithms/C++: sorting/insertion_sort.cpp File Reference</title>
|
|
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
|
|
<script type="text/javascript">
|
|
DoxygenAwesomeDarkModeToggle.init()
|
|
</script>
|
|
<script type="text/javascript" src="../../clipboard.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<script type="text/javascript" src="../../cookie.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/javascript">
|
|
window.MathJax = {
|
|
options: {
|
|
ignoreHtmlClass: 'tex2jax_ignore',
|
|
processHtmlClass: 'tex2jax_process'
|
|
},
|
|
loader: {
|
|
load: ['[tex]/ams']
|
|
},
|
|
tex: {
|
|
macros: {},
|
|
packages: ['base','configmacros','ams']
|
|
}
|
|
};
|
|
</script>
|
|
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
|
|
<td id="projectalign">
|
|
<div id="projectname">TheAlgorithms/C++<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">All the algorithms implemented in C++</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.14.0 -->
|
|
<script type="text/javascript">
|
|
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
|
</script>
|
|
<script type="text/javascript">
|
|
$(function() { codefold.init(); });
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search',true);
|
|
$(function() { init_search(); });
|
|
});
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
$(function(){initNavTree('dd/d0d/insertion__sort_8cpp.html','../../',''); });
|
|
</script>
|
|
<div id="container">
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="headertitle"><div class="title">insertion_sort.cpp File Reference</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p><a href="https://en.wikipedia.org/wiki/Insertion_sort" target="_blank">Insertion Sort Algorithm (Insertion Sort)</a>
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <algorithm></code><br />
|
|
<code>#include <cassert></code><br />
|
|
<code>#include <iostream></code><br />
|
|
<code>#include <vector></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for insertion_sort.cpp:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../db/df0/insertion__sort_8cpp__incl.svg" width="350" height="111"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
<p><a href="../../dd/d0d/insertion__sort_8cpp_source.html">Go to the source code of this file.</a></p>
|
|
<table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
|
|
Namespaces</h2></td></tr>
|
|
<tr class="memitem:sorting" id="r_sorting"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d91/namespacesorting.html">sorting</a></td></tr>
|
|
<tr class="memdesc:d5/d91/namespacesorting"><td class="mdescLeft"> </td><td class="mdescRight">for working with vectors <br /></td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a78cb2f3b97b6db2c062b2a1df05c9ea9" id="r_a78cb2f3b97b6db2c062b2a1df05c9ea9"><td class="memTemplParams" colspan="2">template<typename T> </td></tr>
|
|
<tr class="memitem:a78cb2f3b97b6db2c062b2a1df05c9ea9 template"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a> (T *arr, int n)</td></tr>
|
|
<tr class="memdesc:a78cb2f3b97b6db2c062b2a1df05c9ea9"><td class="mdescLeft"> </td><td class="mdescRight">Insertion Sort Function. <br /></td></tr>
|
|
<tr class="memitem:a8fe6bac9e03f58abcc2ce26ef3de1b5f" id="r_a8fe6bac9e03f58abcc2ce26ef3de1b5f"><td class="memTemplParams" colspan="2">template<typename T> </td></tr>
|
|
<tr class="memitem:a8fe6bac9e03f58abcc2ce26ef3de1b5f template"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d91/namespacesorting.html#a8fe6bac9e03f58abcc2ce26ef3de1b5f">sorting::insertionSort</a> (std::vector< T > *arr)</td></tr>
|
|
<tr class="memdesc:a8fe6bac9e03f58abcc2ce26ef3de1b5f"><td class="mdescLeft"> </td><td class="mdescRight">Insertion Sort for a vector. <br /></td></tr>
|
|
<tr class="memitem:a59914553f24088342c139645a02a8a49" id="r_a59914553f24088342c139645a02a8a49"><td class="memTemplParams" colspan="2">template<typename T> </td></tr>
|
|
<tr class="memitem:a59914553f24088342c139645a02a8a49 template"><td class="memItemLeft" align="right" valign="top">static void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a59914553f24088342c139645a02a8a49">create_random_array</a> (T *arr, int N)</td></tr>
|
|
<tr class="memdesc:a59914553f24088342c139645a02a8a49"><td class="mdescLeft"> </td><td class="mdescRight">Create a random array objecthelper function to create a random array. <br /></td></tr>
|
|
<tr class="memitem:a88ec9ad42717780d6caaff9d3d6977f9" id="r_a88ec9ad42717780d6caaff9d3d6977f9"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a88ec9ad42717780d6caaff9d3d6977f9">tests</a> ()</td></tr>
|
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/Insertion_sort" target="_blank">Insertion Sort Algorithm (Insertion Sort)</a> </p>
|
|
<p>Insertion sort is a simple sorting algorithm that builds the final sorted array one at a time. It is much less efficient compared to other sorting algorithms like heap sort, merge sort or quick sort. However it has several advantages such as</p><ol type="1">
|
|
<li>Easy to implement</li>
|
|
<li>For small set of data it is quite efficient</li>
|
|
<li>More efficient that other Quadratic complexity algorithms like Selection sort or bubble sort.</li>
|
|
<li>It's stable that is it does not change the relative order of elements with equal keys</li>
|
|
<li>Works on hand means it can sort the array or list as it receives.</li>
|
|
</ol>
|
|
<p>It is based on the same idea that people use to sort the playing cards in their hands. the algorithms goes in the manner that we start iterating over the array of elements as soon as we find a unsorted element that is a misplaced element we place it at a sorted position.</p>
|
|
<p>Example execution steps:</p><ol type="1">
|
|
<li>Suppose initially we have <p class="formulaDsp">
|
|
\begin{bmatrix}4 &3 &2 &5 &1\end{bmatrix}
|
|
</p>
|
|
</li>
|
|
<li>We start traversing from 4 till we reach 1 when we reach at 3 we find that it is misplaced so we take 3 and place it at a correct position thus the array will become <p class="formulaDsp">
|
|
\begin{bmatrix}3 &4 &2 &5 &1\end{bmatrix}
|
|
</p>
|
|
</li>
|
|
<li>In the next iteration we are at 2 we find that this is also misplaced so we place it at the correct sorted position thus the array in this iteration becomes <p class="formulaDsp">
|
|
\begin{bmatrix}2 &3 &4 &5 &1\end{bmatrix}
|
|
</p>
|
|
</li>
|
|
<li>We do not do anything with 5 and move on to the next iteration and select 1 which is misplaced and place it at correct position. Thus, we have <p class="formulaDsp">
|
|
\begin{bmatrix}1 &2 &3 &4 &5\end{bmatrix}
|
|
</p>
|
|
</li>
|
|
</ol>
|
|
|
|
<p class="definition">Definition in file <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html">insertion_sort.cpp</a>.</p>
|
|
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
|
|
<a id="a59914553f24088342c139645a02a8a49" name="a59914553f24088342c139645a02a8a49"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a59914553f24088342c139645a02a8a49">◆ </a></span>create_random_array()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<div class="memtemplate">
|
|
template<typename T> </div>
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void create_random_array </td>
|
|
<td>(</td>
|
|
<td class="paramtype">T *</td> <td class="paramname"><span class="paramname"><em>arr</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int</td> <td class="paramname"><span class="paramname"><em>N</em></span> )</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Create a random array objecthelper function to create a random array. </p>
|
|
<dl class="tparams"><dt>Template Parameters</dt><dd>
|
|
<table class="tparams">
|
|
<tr><td class="paramname">T</td><td>type of array </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">arr</td><td>array to fill (must be pre-allocated) </td></tr>
|
|
<tr><td class="paramname">N</td><td>number of array elements </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html#l00101">101</a> of file <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html">insertion_sort.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 101</span> {</div>
|
|
<div class="line"><span class="lineno"> 102</span> <span class="keywordflow">while</span> (N--) {</div>
|
|
<div class="line"><span class="lineno"> 103</span> <span class="keywordtype">double</span> r = (std::rand() % 10000 - 5000) / 100.f;</div>
|
|
<div class="line"><span class="lineno"> 104</span> arr[N] = <span class="keyword">static_cast<</span>T<span class="keyword">></span>(r);</div>
|
|
<div class="line"><span class="lineno"> 105</span> }</div>
|
|
<div class="line"><span class="lineno"> 106</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Main Function </p>
|
|
<p>Running predefined tests to test algorithm</p>
|
|
<p>For user insteraction</p>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html#l00150">150</a> of file <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html">insertion_sort.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 150</span> {</div>
|
|
<div class="line"><span class="lineno"> 152</span> <a class="code hl_function" href="#a88ec9ad42717780d6caaff9d3d6977f9">tests</a>();</div>
|
|
<div class="line"><span class="lineno"> 153</span></div>
|
|
<div class="line"><span class="lineno"> 155</span> <span class="keywordtype">size_t</span> n;</div>
|
|
<div class="line"><span class="lineno"> 156</span> std::cout << <span class="stringliteral">"Enter the length of your array (0 to exit): "</span>;</div>
|
|
<div class="line"><span class="lineno"> 157</span> std::cin >> n;</div>
|
|
<div class="line"><span class="lineno"> 158</span> <span class="keywordflow">if</span> (n == 0) {</div>
|
|
<div class="line"><span class="lineno"> 159</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 160</span> }</div>
|
|
<div class="line"><span class="lineno"> 161</span> </div>
|
|
<div class="line"><span class="lineno"> 162</span> <span class="keywordtype">int</span> *arr = <span class="keyword">new</span> <span class="keywordtype">int</span>[n];</div>
|
|
<div class="line"><span class="lineno"> 163</span> std::cout << <span class="stringliteral">"Enter any "</span> << n << <span class="stringliteral">" Numbers for Unsorted Array : "</span>;</div>
|
|
<div class="line"><span class="lineno"> 164</span> </div>
|
|
<div class="line"><span class="lineno"> 165</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < n; i++) {</div>
|
|
<div class="line"><span class="lineno"> 166</span> std::cin >> arr[i];</div>
|
|
<div class="line"><span class="lineno"> 167</span> }</div>
|
|
<div class="line"><span class="lineno"> 168</span> </div>
|
|
<div class="line"><span class="lineno"> 169</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(arr, n);</div>
|
|
<div class="line"><span class="lineno"> 170</span> </div>
|
|
<div class="line"><span class="lineno"> 171</span> std::cout << <span class="stringliteral">"\nSorted Array : "</span>;</div>
|
|
<div class="line"><span class="lineno"> 172</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < n; i++) {</div>
|
|
<div class="line"><span class="lineno"> 173</span> std::cout << arr[i] << <span class="stringliteral">" "</span>;</div>
|
|
<div class="line"><span class="lineno"> 174</span> }</div>
|
|
<div class="line"><span class="lineno"> 175</span> </div>
|
|
<div class="line"><span class="lineno"> 176</span> std::cout << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 177</span> <span class="keyword">delete</span>[] arr;</div>
|
|
<div class="line"><span class="lineno"> 178</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 179</span>}</div>
|
|
<div class="ttc" id="ainsertion__sort_8cpp_html_a88ec9ad42717780d6caaff9d3d6977f9"><div class="ttname"><a href="#a88ec9ad42717780d6caaff9d3d6977f9">tests</a></div><div class="ttdeci">void tests()</div><div class="ttdef"><b>Definition</b> <a href="../../dd/d0d/insertion__sort_8cpp_source.html#l00109">insertion_sort.cpp:109</a></div></div>
|
|
<div class="ttc" id="anamespacesorting_html_a78cb2f3b97b6db2c062b2a1df05c9ea9"><div class="ttname"><a href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a></div><div class="ttdeci">void insertionSort(T *arr, int n)</div><div class="ttdoc">Insertion Sort Function.</div><div class="ttdef"><b>Definition</b> <a href="../../dd/d0d/insertion__sort_8cpp_source.html#l00059">insertion_sort.cpp:59</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a88ec9ad42717780d6caaff9d3d6977f9" name="a88ec9ad42717780d6caaff9d3d6977f9"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a88ec9ad42717780d6caaff9d3d6977f9">◆ </a></span>tests()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void tests </td>
|
|
<td>(</td>
|
|
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Test Cases to test algorithm </p>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html#l00109">109</a> of file <a class="el" href="../../dd/d0d/insertion__sort_8cpp_source.html">insertion_sort.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 109</span> {</div>
|
|
<div class="line"><span class="lineno"> 110</span> <span class="keywordtype">int</span> arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};</div>
|
|
<div class="line"><span class="lineno"> 111</span> std::cout << <span class="stringliteral">"Test 1... "</span>;</div>
|
|
<div class="line"><span class="lineno"> 112</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(arr1, 10);</div>
|
|
<div class="line"><span class="lineno"> 113</span> assert(std::is_sorted(arr1, arr1 + 10));</div>
|
|
<div class="line"><span class="lineno"> 114</span> std::cout << <span class="stringliteral">"passed"</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 115</span> </div>
|
|
<div class="line"><span class="lineno"> 116</span> <span class="keywordtype">int</span> arr2[5] = {5, -3, 7, -2, 1};</div>
|
|
<div class="line"><span class="lineno"> 117</span> std::cout << <span class="stringliteral">"Test 2... "</span>;</div>
|
|
<div class="line"><span class="lineno"> 118</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(arr2, 5);</div>
|
|
<div class="line"><span class="lineno"> 119</span> assert(std::is_sorted(arr2, arr2 + 5));</div>
|
|
<div class="line"><span class="lineno"> 120</span> std::cout << <span class="stringliteral">"passed"</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 121</span> </div>
|
|
<div class="line"><span class="lineno"> 122</span> <span class="keywordtype">float</span> arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};</div>
|
|
<div class="line"><span class="lineno"> 123</span> std::cout << <span class="stringliteral">"Test 3... "</span>;</div>
|
|
<div class="line"><span class="lineno"> 124</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(arr3, 5);</div>
|
|
<div class="line"><span class="lineno"> 125</span> assert(std::is_sorted(arr3, arr3 + 5));</div>
|
|
<div class="line"><span class="lineno"> 126</span> std::cout << <span class="stringliteral">"passed"</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 127</span> </div>
|
|
<div class="line"><span class="lineno"> 128</span> std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});</div>
|
|
<div class="line"><span class="lineno"> 129</span> std::cout << <span class="stringliteral">"Test 4... "</span>;</div>
|
|
<div class="line"><span class="lineno"> 130</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(&arr4);</div>
|
|
<div class="line"><span class="lineno"> 131</span> assert(std::is_sorted(std::begin(arr4), std::end(arr4)));</div>
|
|
<div class="line"><span class="lineno"> 132</span> std::cout << <span class="stringliteral">"passed"</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 133</span> </div>
|
|
<div class="line"><span class="lineno"> 134</span> <span class="keywordtype">int</span> arr5[50];</div>
|
|
<div class="line"><span class="lineno"> 135</span> std::cout << <span class="stringliteral">"Test 5... "</span>;</div>
|
|
<div class="line"><span class="lineno"> 136</span> <a class="code hl_function" href="#a59914553f24088342c139645a02a8a49">create_random_array</a>(arr5, 50);</div>
|
|
<div class="line"><span class="lineno"> 137</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(arr5, 50);</div>
|
|
<div class="line"><span class="lineno"> 138</span> assert(std::is_sorted(arr5, arr5 + 50));</div>
|
|
<div class="line"><span class="lineno"> 139</span> std::cout << <span class="stringliteral">"passed"</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 140</span> </div>
|
|
<div class="line"><span class="lineno"> 141</span> <span class="keywordtype">float</span> arr6[50];</div>
|
|
<div class="line"><span class="lineno"> 142</span> std::cout << <span class="stringliteral">"Test 6... "</span>;</div>
|
|
<div class="line"><span class="lineno"> 143</span> <a class="code hl_function" href="#a59914553f24088342c139645a02a8a49">create_random_array</a>(arr6, 50);</div>
|
|
<div class="line"><span class="lineno"> 144</span> <a class="code hl_function" href="../../d5/d91/namespacesorting.html#a78cb2f3b97b6db2c062b2a1df05c9ea9">sorting::insertionSort</a>(arr6, 50);</div>
|
|
<div class="line"><span class="lineno"> 145</span> assert(std::is_sorted(arr6, arr6 + 50));</div>
|
|
<div class="line"><span class="lineno"> 146</span> std::cout << <span class="stringliteral">"passed"</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 147</span>}</div>
|
|
<div class="ttc" id="ainsertion__sort_8cpp_html_a59914553f24088342c139645a02a8a49"><div class="ttname"><a href="#a59914553f24088342c139645a02a8a49">create_random_array</a></div><div class="ttdeci">static void create_random_array(T *arr, int N)</div><div class="ttdoc">Create a random array objecthelper function to create a random array.</div><div class="ttdef"><b>Definition</b> <a href="../../dd/d0d/insertion__sort_8cpp_source.html#l00101">insertion_sort.cpp:101</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div id="page-nav" class="page-nav-panel">
|
|
<div id="page-nav-resize-handle"></div>
|
|
<div id="page-nav-tree">
|
|
<div id="page-nav-contents">
|
|
</div><!-- page-nav-contents -->
|
|
</div><!-- page-nav-tree -->
|
|
</div><!-- page-nav -->
|
|
</div><!-- container -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a href="../../dir_bb1b521853a9c46347182a9d10420771.html">sorting</a></li><li class="navelem"><a href="../../dd/d0d/insertion__sort_8cpp.html">insertion_sort.cpp</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|