Hi there,
I am very new to programming, so please bear with me.
I have a console application running that takes inputs from user and sorts it in ascending order using Merge sort. At the end of the console, I want to use insertion sort to create one single sorted list that will be outputted to the user in ascending order. Could someone help me do this using insertion sort. This is my code below. Sorry for all the comments, I use it to learn.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MergeSort
- {
- class MergeSort
- {
- static public void MainMerge(int[] numbers, int left, int mid, int right)
- {
- int[] temp = new int[25];
- int i, stop, number, position;
-
- stop = (mid - 1);
- position = left;
- number = (right - left + 1);
-
- while ((left <= stop) && (mid <= right))
- {
- if (numbers[left] <= numbers[mid])
- temp[position++] = numbers[left++];
- else
- temp[position++] = numbers[mid++];
- }
-
- while (left <= stop)
- temp[position++] = numbers[left++];
-
- while (mid <= right)
- temp[position++] = numbers[mid++];
-
- for (i = 0; i < number; i++)
- {
- numbers[right] = temp[right];
- right--;
- }
- }
-
- static public void SortMerge(int[] numbers, int left, int right)
- {
- int middle;
-
- if (right > left)
- {
- middle = (right + left) / 2;
- SortMerge(numbers, left, middle);
- SortMerge(numbers, (middle + 1), right);
-
- MainMerge(numbers, left, (middle + 1), right);
- }
- }
-
- static void Main(string[] args)
-
-
- {
- Console.WriteLine("\nHi there! Welcome to Merge Sorting! Lets get started:");
- Console.WriteLine("\n\nHow many numbers would you like to sort in the FIRST set of numbers? ");
- int totalElements = Convert.ToInt32(Console.ReadLine());
- int[] elementsInArray = new int[totalElements];
- for (int i = 0; i < totalElements; i++)
-
- {
-
- Console.Write("\nPlease enter number #" + (i + 1).ToString() + " >>> ");
- elementsInArray[i] = Convert.ToInt32(Console.ReadLine());
- }
- Console.Write("\n\nYour unsorted inputs in the array are: ");
- Console.Write("\n");
- for (int x = 0; x < totalElements; x++)
- {
- Console.Write(elementsInArray[x] + " ");
- Console.Write("\n");
- }
-
- {
- Console.WriteLine("\n\nWooooHooooo! Your FIRST list of numbers has been MergeSorted using the recursive method:");
- SortMerge(elementsInArray, 0, totalElements - 1);
- for (int y = 0; y < totalElements; y++)
- Console.WriteLine(elementsInArray[y]);
- {
-
- }
-
-
-
-
-
- Console.WriteLine("\nNow let's merge sort list two now:");
- Console.WriteLine("\n\nHow many numbers would you like to sort in this SECOND list? ");
- int totalElementsTwo = Convert.ToInt32(Console.ReadLine());
- int[] elementsInArrayTwo = new int[totalElementsTwo];
- for (int i = 0; i < totalElementsTwo; i++)
-
- {
-
- Console.Write("\nPlease enter number #" + (i + 1).ToString() + " >>> ");
- elementsInArrayTwo[i] = Convert.ToInt32(Console.ReadLine());
- }
- Console.Write("\n\nYour unsorted inputs in the array are: ");
- Console.Write("\n");
- for (int p = 0; p < totalElementsTwo; p++)
- {
- Console.Write(elementsInArrayTwo[p] + " ");
- Console.Write("\n");
- }
-
- {
- Console.WriteLine("\nWow! Your SECOND list of numbers has been MergeSorted using the recursive method:");
- SortMerge(elementsInArrayTwo, 0, totalElementsTwo - 1);
- for (int q = 0; q < totalElementsTwo; q++)
- Console.WriteLine(elementsInArrayTwo[q]);
-
- Console.WriteLine("\nThis is both List A and List B sorted separately:\n");
- SortMerge(elementsInArray, 0, totalElements - 1);
- {
- Console.WriteLine("LIST A:");
- }
- for (int y = 0; y < totalElements; y++)
- Console.WriteLine(elementsInArray[y]);
- {
- Console.WriteLine("\nLIST B:");
- }
- for (int q = 0; q < totalElementsTwo; q++)
- Console.WriteLine(elementsInArrayTwo[q]);
- {
- Console.WriteLine("\nNow its time to sort both lists using insertion sort. \nPresss any key to Block sort your Above 2 Lists:");
- }
- Console.ReadKey();
-
- }
- }
- }
- }
- }