Program to Sort two array and Merging of these two sorted array
using System;
using System.Text;
namespace ProblemPractice
{
class Program2
{
//Merge Sorted Array
public static void Main(String[] args)
{
int[] a, b, c;
a = new int[] { 2,1,8,5,3,7 };
b = new int[] { 6, 9, 0, 4, 15, 11 };
Array.Sort(a);
Array.Sort(b);
c = MergeArray(a, b);
int clen = c.Length;
Console.WriteLine("Sorted and Merged Array:");
for (int i = 0; i < clen; i++)
{
Console.Write(c[i] + " ");
}
Console.ReadKey();
}
private static int[] MergeArray(int[] a, int[] b)
{
int alen, blen, m, n, i;
m = a.Length;
n = b.Length;
int[] c = new int[m + n];
alen = 0;
blen = 0;
for (i = 0; i < Math.Min(m, n); i++)
{
if (a[alen] <= b[blen])
{
c[i] = a[alen++];
}
else
{
c[i] = b[blen++];
}
}
while (m > alen)
{
c[i++] = a[alen++];
}
while (n > blen)
{
c[i++] = b[blen++];
}
return c;
}
}
}
Merge Sorted Array |
using System;
using System.Text;
namespace ProblemPractice
{
class Program2
{
//Merge Sorted Array
public static void Main(String[] args)
{
int[] a, b, c;
a = new int[] { 2,1,8,5,3,7 };
b = new int[] { 6, 9, 0, 4, 15, 11 };
Array.Sort(a);
Array.Sort(b);
c = MergeArray(a, b);
int clen = c.Length;
Console.WriteLine("Sorted and Merged Array:");
for (int i = 0; i < clen; i++)
{
Console.Write(c[i] + " ");
}
Console.ReadKey();
}
private static int[] MergeArray(int[] a, int[] b)
{
int alen, blen, m, n, i;
m = a.Length;
n = b.Length;
int[] c = new int[m + n];
alen = 0;
blen = 0;
for (i = 0; i < Math.Min(m, n); i++)
{
if (a[alen] <= b[blen])
{
c[i] = a[alen++];
}
else
{
c[i] = b[blen++];
}
}
while (m > alen)
{
c[i++] = a[alen++];
}
while (n > blen)
{
c[i++] = b[blen++];
}
return c;
}
}
}
No comments:
Post a Comment