9
Answers

Worst Case of Insertion Sort in C#

Nilesh Jadav

Nilesh Jadav

8y
550
1
I had written Insertion Sort Program in C#, where I got Best Case by sorting the array in ascending form, now I want to get the worst case of the Insertion sort by making that ascending from of sorted output in descending form. How to change my code to do that , Please help me with full code.<br /><br />I am sharing my code below:<div class="dp-highlighter"><ol class="dp-c" start="1"><li class="alt"><span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;InsertionSorting()&nbsp;&nbsp;</span></span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span>Console.WriteLine(<span class="string">"Insertion&nbsp;Sort:"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>Console.Write(<span class="string">"\n"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">int</span><span>&nbsp;[]&nbsp;arr&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;</span><span class="keyword">int</span><span>[3];&nbsp;&nbsp;</span></span></li><li class=""><span>Random&nbsp;rn&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Random();&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">for</span><span>&nbsp;(</span><span class="keyword">int</span><span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;3;&nbsp;i++)&nbsp;&nbsp;</span></span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span>arr[i]&nbsp;=&nbsp;rn.Next(10000);&nbsp;&nbsp;</span></li><li class=""><span>Console.WriteLine(<span class="string">"Input&nbsp;Array&nbsp;Element&nbsp;["</span><span>&nbsp;+(i&nbsp;+&nbsp;1).ToString()&nbsp;+&nbsp;</span><span class="string">"]&nbsp;"</span><span>&nbsp;+&nbsp;arr[i]);&nbsp;&nbsp;</span></span></li><li class="alt"><span>Console.Write(<span class="string">"\n"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">int</span><span>&nbsp;count&nbsp;=&nbsp;0;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">for</span><span>&nbsp;(</span><span class="keyword">int</span><span>&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;&nbsp;3;&nbsp;i++)&nbsp;&nbsp;</span></span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">int</span><span>&nbsp;j&nbsp;=&nbsp;i;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">while</span><span>&nbsp;(j&nbsp;&gt;&nbsp;0)&nbsp;&nbsp;</span></span></li><li class="alt"><span>{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">if</span><span>&nbsp;(arr[j&nbsp;-&nbsp;1]&nbsp;&gt;&nbsp;arr[j])&nbsp;&nbsp;</span></span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">int</span><span>&nbsp;temp&nbsp;=&nbsp;arr[j&nbsp;-&nbsp;1];&nbsp;&nbsp;</span></span></li><li class=""><span>arr[j&nbsp;-&nbsp;1]&nbsp;=&nbsp;arr[j];&nbsp;&nbsp;</span></li><li class="alt"><span>arr[j]&nbsp;=&nbsp;temp;&nbsp;&nbsp;</span></li><li class=""><span>j--;&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">else</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">break</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>count++;&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">for</span><span>&nbsp;(</span><span class="keyword">int</span><span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;3;&nbsp;i++)&nbsp;&nbsp;</span></span></li><li class="alt"><span>{&nbsp;&nbsp;</span></li><li class=""><span>Console.WriteLine(<span class="string">"\n&nbsp;The&nbsp;Sorted&nbsp;Array&nbsp;is&nbsp;["</span><span>&nbsp;+&nbsp;(i&nbsp;+&nbsp;1).ToString()&nbsp;+&nbsp;</span><span class="string">"]&nbsp;"</span><span>&nbsp;+&nbsp;arr[i]);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>Console.Write(<span class="string">"\n"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>Console.WriteLine(<span class="string">"\nNumber&nbsp;of&nbsp;Comparision="</span><span>&nbsp;+&nbsp;count.ToString());&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span><span class="comment">//for&nbsp;worst&nbsp;case:</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>Console.Read();&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;Main(</span><span class="keyword">string</span><span>[]&nbsp;args)&nbsp;&nbsp;</span></span></li><li class="alt"><span>{&nbsp;&nbsp;</span></li><li class=""><span>InsertionSorting();&nbsp;&nbsp;</span></li><li class="alt"><span>Console.ReadLine();&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp; <br /></span></li></ol></div><br /><br /><div> </div><div>&nbsp;<strong>**I tried Like this :** </strong></div><br /> <div class="dp-highlighter"><ol class="dp-c" start="1"><li class="alt"><span><span class="keyword">for</span><span>&nbsp;(</span><span class="keyword">int</span><span>&nbsp;i&nbsp;=&nbsp;arr.Length;&nbsp;i&nbsp;&lt;&nbsp;0;&nbsp;i--)&nbsp;&nbsp;</span></span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">int</span><span>&nbsp;j&nbsp;=&nbsp;i;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">while</span><span>&nbsp;(j&nbsp;&gt;&nbsp;0)&nbsp;&nbsp;</span></span></li><li class="alt"><span>{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">if</span><span>&nbsp;(arr[j&nbsp;-&nbsp;1]&nbsp;&lt;&nbsp;arr[j])&nbsp;&nbsp;</span></span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">int</span><span>&nbsp;temp&nbsp;=&nbsp;arr[j&nbsp;-&nbsp;1];&nbsp;&nbsp;</span></span></li><li class=""><span>arr[j&nbsp;-&nbsp;1]&nbsp;=&nbsp;arr[j];&nbsp;&nbsp;</span></li><li class="alt"><span>arr[j]&nbsp;=&nbsp;temp;&nbsp;&nbsp;</span></li><li class=""><span>j++;&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">else</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">break</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>Console.WriteLine(<span class="string">"\n&nbsp;The&nbsp;Sorted&nbsp;Array&nbsp;is&nbsp;["</span><span>&nbsp;+&nbsp;(i&nbsp;+&nbsp;1).ToString()&nbsp;+&nbsp;</span><span class="string">"]&nbsp;"</span><span>&nbsp;+&nbsp;arr[i]);&nbsp;&nbsp;</span></span></li><li class=""><span>Console.Read();&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp; <br /></span></li></ol><p><br /></p><div>&nbsp;</div></div>
Answers (9)