I am sharing my code below:
- static void InsertionSorting()
- {
- Console.WriteLine("Insertion Sort:");
- Console.Write("\n");
- int [] arr = new int[3];
- Random rn = new Random();
- for (int i = 0; i < 3; i++)
- {
- arr[i] = rn.Next(10000);
- Console.WriteLine("Input Array Element [" +(i + 1).ToString() + "] " + arr[i]);
- Console.Write("\n");
- }
- int count = 0;
- for (int i = 1; i < 3; i++)
- {
- int j = i;
- while (j > 0)
- {
- if (arr[j - 1] > arr[j])
- {
- int temp = arr[j - 1];
- arr[j - 1] = arr[j];
- arr[j] = temp;
- j--;
- }
- else
- break;
- count++;
- }
- }
- for (int i = 0; i < 3; i++)
- {
- Console.WriteLine("\n The Sorted Array is [" + (i + 1).ToString() + "] " + arr[i]);
- }
- Console.Write("\n");
- Console.WriteLine("\nNumber of Comparision=" + count.ToString());
- //for worst case:
- Console.Read();
- }
- static void Main(string[] args)
- {
- InsertionSorting();
- Console.ReadLine();
- }
- }
**I tried Like this :**
- for (int i = arr.Length; i < 0; i--)
- {
- int j = i;
- while (j > 0)
- {
- if (arr[j - 1] < arr[j])
- {
- int temp = arr[j - 1];
- arr[j - 1] = arr[j];
- arr[j] = temp;
- j++;
- }
- else
- break;
- }
- Console.WriteLine("\n The Sorted Array is [" + (i + 1).ToString() + "] " + arr[i]);
- Console.Read();
- }


Midhun TpRank #388 · 6.5K reputationPosted Sep 27, 2016, 8:42 PM EDT
Guest UserRank #2,002 · 726 reputationPosted Sep 27, 2016, 5:35 AM EDT
Krishna Rajput SinghRank #605 · 4K reputationPosted Sep 27, 2016, 6:02 AM EDT
Nilesh JadavRank #109 · 20.3K reputationPosted Sep 27, 2016, 7:11 AM EDT
@Krishna Kumar
Well the case is not as you are defining in your links :
I had a sorted output in ascending form and Now I want to unsort it in descending form.
theLizardRank #1,079 · 1.8K reputationPosted Sep 27, 2016, 6:16 PM EDT
{
Console.WriteLine("\n The Sorted Array is [" + (i).ToString() + "] " + arr[i-1]);
}
Nilesh JadavRank #109 · 20.3K reputationPosted Sep 27, 2016, 7:18 PM EDT
If you see in debug mode, you will get to know that my worst case is unable to print, might be some mistake in for loop or anywhere else, I can't find it out. For more elaboration I am attaching my console to know you the thing.
Here in the worst case : I want to print the descending form of above array, and besides this I also want to print the No.of comparison , similar you see in best case.
Can you help me to modify the code? or just give me the code, I really need this today !
Thank you !
Nilesh JadavRank #109 · 20.3K reputationPosted Sep 28, 2016, 12:01 AM EDT
Can you further help me ?
I also need the time complexity for both the cases - how much both the loops are taking time to execute ?
Besides I also need the space complexity - how much both the loops are taking memory ?
Thank you so much for the help !
Midhun TpRank #388 · 6.5K reputationPosted Sep 28, 2016, 12:36 AM EDT
theLizardRank #1,079 · 1.8K reputationPosted Sep 28, 2016, 3:46 PM EDT
Add this code to yours here... You do not need to do what Midhun suggests at all, it is a waste of time since you already have the array sorted in ascending order, all that needs to be done is print the array in reverse order... That is why I gave you the following example...
for (int i = arr.Length; i > 0; i--)
{
Console.WriteLine("\n The Sorted Array is [" + (i).ToString() + "] " + arr[i-1]);
}