Include dependency graph for cycle_sort2.cpp:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../da/de9/cycle__sort2_8cpp__incl.svg"width="170"height="112"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<trclass="memitem:af82f47c25011c54bd19ae9cd775e52c1"><tdclass="memTemplParams"colspan="2">template<typename T ></td></tr>
<trclass="memitem:af82f47c25011c54bd19ae9cd775e52c1"><tdclass="memTemplItemLeft"align="right"valign="top">void </td><tdclass="memTemplItemRight"valign="bottom"><aclass="el"href="../../da/d87/cycle__sort2_8cpp.html#af82f47c25011c54bd19ae9cd775e52c1">sorting::cycle_sort::swap</a> (T &a, T &b)</td></tr>
<trclass="memdesc:af82f47c25011c54bd19ae9cd775e52c1"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Function to perform swapping. <ahref="../../da/d87/cycle__sort2_8cpp.html#af82f47c25011c54bd19ae9cd775e52c1">More...</a><br/></td></tr>
<trclass="memitem:a4c52eee647a17095f4893f6258893e15"><tdclass="memTemplParams"colspan="2">template<typename T ></td></tr>
<trclass="memitem:a4c52eee647a17095f4893f6258893e15"><tdclass="memTemplItemLeft"align="right"valign="top">void </td><tdclass="memTemplItemRight"valign="bottom"><aclass="el"href="../../da/d87/cycle__sort2_8cpp.html#a4c52eee647a17095f4893f6258893e15">sorting::cycle_sort::cycleSort</a> (<aclass="elRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< T >&arr, int n)</td></tr>
<trclass="memdesc:a4c52eee647a17095f4893f6258893e15"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Function to perform cycle sort. <ahref="../../da/d87/cycle__sort2_8cpp.html#a4c52eee647a17095f4893f6258893e15">More...</a><br/></td></tr>
<divclass="textblock"><p>Cycle sort is based on the idea that array to be sorted can be divided into cycles, which can individually be rotated to give a sorted result. </p>
<p>It is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of memory writes to the original array. Each value is either written zero times, if it’s already in its correct position, or written one time to its correct position.</p>
<p>The array is divided into cycles. We begin with the cycle containing the first element. Find the correct position of first element and place it at its correct position, say j. Now we consider the old value of arr[j] and find its correct position. This process is continued until all the elements of the current cycle are placed at their correct position, i.e. until the starting point of the cycle is reached.</p>
<tdclass="paramtype"><aclass="elRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< T >& </td>
<tdclass="paramname"><em>arr</em>, </td>
</tr>
<tr>
<tdclass="paramkey"></td>
<td></td>
<tdclass="paramtype">int </td>
<tdclass="paramname"><em>n</em> </td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><divclass="memdoc">
<p>Function to perform cycle sort. </p>
<dlclass="params"><dt>Parameters</dt><dd>
<tableclass="params">
<tr><tdclass="paramname">arr</td><td>vector of elements. </td></tr>
<tr><tdclass="paramname">n</td><td>size of the vector </td></tr>
<divclass="line"><aname="l00061"></a><spanclass="lineno"> 61</span> <spanclass="comment">// traverse array elements and put it at it's correct positon</span></div>
<divclass="line"><aname="l00067"></a><spanclass="lineno"> 67</span> <spanclass="keywordtype">int</span> pos = cycle_start; <spanclass="comment">// Find position where we put the item.</span></div>
<divclass="line"><aname="l00068"></a><spanclass="lineno"> 68</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = cycle_start + 1; i < n; i++) <spanclass="comment">// we basically count all smaller elements</span></div>
<divclass="line"><aname="l00069"></a><spanclass="lineno"> 69</span> <spanclass="keywordflow">if</span> (arr[i] < item) <spanclass="comment">// on the right side of the item.</span></div>
<divclass="line"><aname="l00073"></a><spanclass="lineno"> 73</span> <spanclass="keywordflow">continue</span>; <spanclass="comment">// If item is already in correct position</span></div>
<divclass="line"><aname="l00080"></a><spanclass="lineno"> 80</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/algorithm/swap.html">swap</a>(item, arr[pos]); <spanclass="comment">// place the item at it's correct position</span></div>
<divclass="line"><aname="l00087"></a><spanclass="lineno"> 87</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = cycle_start + 1; i < n; i++) <spanclass="comment">// Find position where we put the element</span></div>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/algorithm/swap.html">swap</a>(item, arr[pos]); <spanclass="comment">// place the item at it's correct position</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../da/d87/cycle__sort2_8cpp_a4c52eee647a17095f4893f6258893e15_cgraph.svg"width="482"height="95"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../da/d87/cycle__sort2_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg"width="154"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00118"></a><spanclass="lineno"> 118</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"Test 1: After sort-\n"</span>;</div>
<divclass="line"><aname="l00121"></a><spanclass="lineno"> 121</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < size1; i++)</div>
<divclass="line"><aname="l00127"></a><spanclass="lineno"> 127</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"Test 2: After sort-\n"</span>;</div>
<divclass="line"><aname="l00131"></a><spanclass="lineno"> 131</span> <spanclass="keywordflow">for</span> (<spanclass="keywordtype">int</span> i = 0; i < size2; i++)</div>
<divclass="ttc"id="acycle__sort_8cpp_html_ad0cfe2e54b1d3f9d0ca648265d917c6a"><divclass="ttname"><ahref="../../de/d07/cycle__sort_8cpp.html#ad0cfe2e54b1d3f9d0ca648265d917c6a">sorting::cycle_sort::cycleSort</a></div><divclass="ttdeci">std::vector< T > cycleSort(const std::vector< T >&in_arr)</div><divclass="ttdoc">The main function implements cycleSort.</div><divclass="ttdef"><b>Definition:</b> cycle_sort.cpp:39</div></div>
<divclass="ttc"id="avector_html"><divclass="ttname"><ahref="http://en.cppreference.com/w/cpp/container/vector.html">std::vector< int ></a></div></div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<divid="nav-path"class="navpath"><!-- id is needed for treeview function! -->
<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.1 </li>
<gid="a_node2"><axlink:href="../../de/d07/cycle__sort_8cpp.html#ad0cfe2e54b1d3f9d0ca648265d917c6a"target="_top"xlink:title="The main function implements cycleSort.">
<area shape="rect" id="node1" title="Cycle sort is based on the idea that array to be sorted can be divided into cycles,..." alt="" coords="7,5,164,32"/>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.