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
Mohammed Hoorzook
NA
8
2.2k
How to apply insertion sort in from two sorted arrays?
Sep 29 2017 1:34 PM
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:"
);
//Welcome message
Console.WriteLine(
"\n\nHow many numbers would you like to sort in the FIRST set of numbers? "
);
// Ask user to input the number of elements they want to sort.
int
totalElements = Convert.ToInt32(Console.ReadLine());
//Converts user input to integer
int
[] elementsInArray =
new
int
[totalElements];
// An array called "elementsInArray" is created and is assigned to a variable called "numOfelements"
for
(
int
i = 0; i < totalElements; i++)
// A loop is iterated to prompt user to enter their number until "elementsInArray" is equals to " totalElements"
{
Console.Write(
"\nPlease enter number #"
+ (i + 1).ToString() +
" >>> "
);
// Prints on screen a promt that asks user to enter the (n)th element.
elementsInArray[i] = Convert.ToInt32(Console.ReadLine());
// User number entries are parsed into integers.
}
Console.Write(
"\n\nYour unsorted inputs in the array are: "
);
//Print to users a message
Console.Write(
"\n"
);
// Skips a line...
for
(
int
x = 0; x < totalElements; x++)
// A loop is created so that all element are printed until total elements are reached.
{
Console.Write(elementsInArray[x] +
" "
);
//Print users inputs back to the user in a non sorted manner.
Console.Write(
"\n"
);
// Skips a line...
}
{
Console.WriteLine(
"\n\nWooooHooooo! Your FIRST list of numbers has been MergeSorted using the recursive method:"
);
//Print to users a message
SortMerge(elementsInArray, 0, totalElements - 1);
//The SortMerge class is called to do the actual sort. (See class for detailed explanation)
for
(
int
y = 0; y < totalElements; y++)
// a loop is created so that all elements will be printed until we reach "totalElements" value (user selection)
Console.WriteLine(elementsInArray[y]);
//Prints out number in a sorted manner.
{
//Console.WriteLine("\nPress any key to continue...");
}
//Console.ReadKey(); //Await user input to avoid console from closing
//*******************************************************MERGE OPERATION 2************************************************************//
Console.WriteLine(
"\nNow let's merge sort list two now:"
);
//Welcome message
Console.WriteLine(
"\n\nHow many numbers would you like to sort in this SECOND list? "
);
// Ask user to input the number of elements they want to sort.
int
totalElementsTwo = Convert.ToInt32(Console.ReadLine());
//Converts user input to integer
int
[] elementsInArrayTwo =
new
int
[totalElementsTwo];
// An array called "elementsInArray" is created and is assigned to a variable called "numOfelements"
for
(
int
i = 0; i < totalElementsTwo; i++)
// A loop is iterated to prompt user to enter their number until "elementsInArray" is equals to " totalElements"
{
Console.Write(
"\nPlease enter number #"
+ (i + 1).ToString() +
" >>> "
);
// Prints on screen a promt that asks user to enter the (n)th element.
elementsInArrayTwo[i] = Convert.ToInt32(Console.ReadLine());
// User number entries are parsed into integers.
}
Console.Write(
"\n\nYour unsorted inputs in the array are: "
);
//Print to users a message
Console.Write(
"\n"
);
// Skips a line...
for
(
int
p = 0; p < totalElementsTwo; p++)
// A loop is created so that all element are printed until total elements are reached.
{
Console.Write(elementsInArrayTwo[p] +
" "
);
//Print users inputs back to the user in a non sorted manner.
Console.Write(
"\n"
);
// Skips a line...
}
{
Console.WriteLine(
"\nWow! Your SECOND list of numbers has been MergeSorted using the recursive method:"
);
//Print to users a message
SortMerge(elementsInArrayTwo, 0, totalElementsTwo - 1);
//The SortMerge class is called to do the actual sort. (See class for detailed explanation)
for
(
int
q = 0; q < totalElementsTwo; q++)
// a loop is created so that all elements will be printed until we reach "totalElements" value (user selection)
Console.WriteLine(elementsInArrayTwo[q]);
//Prints out number in a sorted manner.
Console.WriteLine(
"\nThis is both List A and List B sorted separately:\n"
);
//Print to users a message
SortMerge(elementsInArray, 0, totalElements - 1);
//The SortMerge class is called to do the actual sort. (See class for detailed explanation)
{
Console.WriteLine(
"LIST A:"
);
}
for
(
int
y = 0; y < totalElements; y++)
// a loop is created so that all elements will be printed until we reach "totalElements" value (user selection)
Console.WriteLine(elementsInArray[y]);
//Prints out number in a sorted manner.
{
Console.WriteLine(
"\nLIST B:"
);
}
for
(
int
q = 0; q < totalElementsTwo; q++)
// a loop is created so that all elements will be printed until we reach "totalElements" value (user selection)
Console.WriteLine(elementsInArrayTwo[q]);
//Prints out number in a sorted manner.
{
Console.WriteLine(
"\nNow its time to sort both lists using insertion sort. \nPresss any key to Block sort your Above 2 Lists:"
);
}
Console.ReadKey();
//Await user input to avoid console from closing
}
}
}
}
}
Reply
Answers (
1
)
How to control somepages functionality of printer in vb.net
Selection sort list of string array