TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Nilesh Jadav
84
23.1k
15.4m
Worst Case of Insertion Sort in C#
Sep 27 2016 9:32 AM
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> </span><span class="keyword">void</span><span> InsertionSorting() </span></span></li><li class=""><span>{ </span></li><li class="alt"><span>Console.WriteLine(<span class="string">"Insertion Sort:"</span><span>); </span></span></li><li class=""><span>Console.Write(<span class="string">"\n"</span><span>); </span></span></li><li class="alt"><span><span class="keyword">int</span><span> [] arr = </span><span class="keyword">new</span><span> </span><span class="keyword">int</span><span>[3]; </span></span></li><li class=""><span>Random rn = <span class="keyword">new</span><span> Random(); </span></span></li><li class="alt"><span><span class="keyword">for</span><span> (</span><span class="keyword">int</span><span> i = 0; i < 3; i++) </span></span></li><li class=""><span>{ </span></li><li class="alt"><span>arr[i] = rn.Next(10000); </span></li><li class=""><span>Console.WriteLine(<span class="string">"Input Array Element ["</span><span> +(i + 1).ToString() + </span><span class="string">"] "</span><span> + arr[i]); </span></span></li><li class="alt"><span>Console.Write(<span class="string">"\n"</span><span>); </span></span></li><li class=""><span>} </span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">int</span><span> count = 0; </span></span></li><li class="alt"><span><span class="keyword">for</span><span> (</span><span class="keyword">int</span><span> i = 1; i < 3; i++) </span></span></li><li class=""><span>{ </span></li><li class="alt"><span><span class="keyword">int</span><span> j = i; </span></span></li><li class=""><span><span class="keyword">while</span><span> (j > 0) </span></span></li><li class="alt"><span>{ </span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">if</span><span> (arr[j - 1] > arr[j]) </span></span></li><li class=""><span>{ </span></li><li class="alt"><span><span class="keyword">int</span><span> temp = arr[j - 1]; </span></span></li><li class=""><span>arr[j - 1] = arr[j]; </span></li><li class="alt"><span>arr[j] = temp; </span></li><li class=""><span>j--; </span></li><li class="alt"><span>} </span></li><li class=""><span><span class="keyword">else</span><span> </span></span></li><li class="alt"><span><span class="keyword">break</span><span>; </span></span></li><li class=""><span> </span></li><li class="alt"><span>count++; </span></li><li class=""><span>} </span></li><li class="alt"><span>} </span></li><li class=""><span><span class="keyword">for</span><span> (</span><span class="keyword">int</span><span> i = 0; i < 3; i++) </span></span></li><li class="alt"><span>{ </span></li><li class=""><span>Console.WriteLine(<span class="string">"\n The Sorted Array is ["</span><span> + (i + 1).ToString() + </span><span class="string">"] "</span><span> + arr[i]); </span></span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span>} </span></li><li class=""><span>Console.Write(<span class="string">"\n"</span><span>); </span></span></li><li class="alt"><span>Console.WriteLine(<span class="string">"\nNumber of Comparision="</span><span> + count.ToString()); </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span><span class="comment">//for worst case:</span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span>Console.Read(); </span></li><li class=""><span>} </span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> Main(</span><span class="keyword">string</span><span>[] args) </span></span></li><li class="alt"><span>{ </span></li><li class=""><span>InsertionSorting(); </span></li><li class="alt"><span>Console.ReadLine(); </span></li><li class=""><span> </span></li><li class="alt"><span>} </span></li><li class=""><span> </span></li><li class="alt"><span>} <br /></span></li></ol></div><br /><br /><div> </div><div> <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> (</span><span class="keyword">int</span><span> i = arr.Length; i < 0; i--) </span></span></li><li class=""><span>{ </span></li><li class="alt"><span><span class="keyword">int</span><span> j = i; </span></span></li><li class=""><span><span class="keyword">while</span><span> (j > 0) </span></span></li><li class="alt"><span>{ </span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">if</span><span> (arr[j - 1] < arr[j]) </span></span></li><li class=""><span>{ </span></li><li class="alt"><span><span class="keyword">int</span><span> temp = arr[j - 1]; </span></span></li><li class=""><span>arr[j - 1] = arr[j]; </span></li><li class="alt"><span>arr[j] = temp; </span></li><li class=""><span>j++; </span></li><li class="alt"><span>} </span></li><li class=""><span><span class="keyword">else</span><span> </span></span></li><li class="alt"><span><span class="keyword">break</span><span>; </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span>} </span></li><li class="alt"><span>Console.WriteLine(<span class="string">"\n The Sorted Array is ["</span><span> + (i + 1).ToString() + </span><span class="string">"] "</span><span> + arr[i]); </span></span></li><li class=""><span>Console.Read(); </span></li><li class="alt"><span>} <br /></span></li></ol><p><br /></p><div> </div></div>
Reply
Answers (
9
)
Use of Variables in different using methods
Fastest way to convert image to Black and white image