double[] merge(double[] firstArr, double[] secondArr)
{
double[] resArr = new double[firstArr.Length + secondArr.Length];
int firstPos = 0, secondPos = 0, resPos = 0;
double a, b;
while (firstPos < firstArr.Length && secondPos < secondArr.Length)
{
a = firstArr[firstPos];
b = secondArr[secondPos];
if (a < b)
{
resArr[resPos++] = a;
firstPos++;
}
else if (a > b)
{
resArr[resPos++] = b;
secondPos++;
}
else
{
resArr[resPos++] = a; // or b
firstPos++;
secondPos++;
}
}
if (firstPos >= firstArr.Length)
{
for (int i = secondPos; i < secondArr.Length; i++)
resArr[resPos++] = secondArr[i];
}
else // secondPos >= secondArr.Length
{
for (int i = firstPos; i < firstArr.Length; i++)
resArr[resPos++] = firstArr[i];
}
Array.Resize(ref resArr, resPos);
return resArr;
}
Comments
Leave a comment