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();
- }
9 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Midhun TpPosted Sep 28, 2016, 12:42 AM
theLizardPosted Sep 28, 2016, 7:46 PM
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]);
}
Midhun TpPosted Sep 28, 2016, 4:36 AM
Nilesh JadavPosted Sep 28, 2016, 4:01 AM
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 !
Nilesh JadavPosted Sep 27, 2016, 11:18 PM
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 !
theLizardPosted Sep 27, 2016, 10:16 PM
{
Console.WriteLine("\n The Sorted Array is [" + (i).ToString() + "] " + arr[i-1]);
}
Nilesh JadavPosted Sep 27, 2016, 11:11 AM
@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.
Krishna Rajput SinghPosted Sep 27, 2016, 10:02 AM
Guest UserPosted Sep 27, 2016, 9:35 AM