class Program
{
static void Main(string[] args)
{
int [] arr1={1,3,5,7,9};
int [] arr2={2,4,6,8,10};
int [] result=new int [arr1.Length+arr2.Length];
Merge merg=new Merge();
merg.Sort(arr1, arr2, result);
for (int i = 0; i < result.Length; i++)
Console.WriteLine(result[i]);
Console.Read();
}
}
/// <summary>
/// 二路归并算法
/// </summary>
class Merge
{
public void Sort(int[] X,int[] Y, int[] Z)
{
int i = 0;
int j = 0;
int q = 0;
//int u = Z.Length;
while (i <= X.Length-1 && j <= Y.Length-1)
{
if (X[i] <= Y[j])
{
Z[q] = X[i];
i++;
}
else
{
Z[q] = Y[j];
j++;
}
q++;
}
while (i <= X.Length-1)
{
Z[q] = X[i];
i++;
q++;
}
while (j <= Y.Length-1)
{
Z[q] = Y[j];
j++;
q++;
}
}
public void Sort(int[] X, int s, int t, int u, int[] Z)
{
int i = s;
int j = t + 1;
int q = s;
while (i <= t && j <= u)
{
if (X[i] <= X[j])
{
Z[q] = X[i];
i++;
}
else
{
Z[q] = X[j];
j++;
}
q++;
}
while (i <= t)
{
Z[q] = X[i];
i++;
q++;
}
while (j <= u)
{
Z[q] = X[j];
j++;
q++;
}
}
}