在 C# 中合并两个数组和 return 排序数组
Merge two arrays and return sorted array in c#
给你一个函数 mergeArrays
,它接受 2 个排序数组作为参数。第一个数组有M
个元素,第二个也有M
个元素,但是容量是2*M
.
函数 mergeArrays
将两个数组作为参数以及 M
。您应该合并第二个数组中的两个数组,以便对结果数组进行排序。
示例测试用例 0:
Input
:
1st array: {3,5,6,9,12,14,18,20,25,28}
2nd array: {30,32,34,36,38,40,42,44,46,48 }
Output: {3,5,6,9,12,14,18,20,25,28,30,32,34,36, 38,40,42,44,46,48}
说明:
如问题中所述,第二个数组包含足够的 space 来容纳第一个数组。 Return 合并后的排序数组。
解决方法可以Linq查询:
int[] first = new int[] { 3, 5, 6, 9, 12, 14, 18, 20, 25, 28 };
int[] second = new int[] { 30, 32, 34, 36, 38, 40, 42, 44, 46, 48 };
int[] result = first
.Concat(second)
.OrderBy(x => x)
.ToArray();
测试
// 3, 5, 6, 9, 12, 14, 18, 20, 25, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48
Console.Write(String.Join(", ", result));
它有效,但很难接受作为家庭作业解决方案。因此,我希望您使用我的实现作为测试参考来详细说明您的自己的代码。
没有linq的解决方案:
int[] array1 = new int[] { 3, 5, 6, 9, 12, 14, 18, 20, 25, 28 };
int[] array2 = new int[] { 30, 32, 34, 36, 38, 40, 42, 44, 46, 48 };
int count1 = array1.Length;
int count2 = array2.Length;
int[] arrayResult = new int[count1 + count2];
int a = 0, b = 0; // indexes in origin arrays
int i = 0; // index in result array
// join
while (a < count1 && b < count2)
{
if (array1[a] <= array2[b])
{
// element in first array at current index 'a'
// is less or equals to element in second array at index 'b'
arrayResult[i++] = array1[a++];
}
else
{
arrayResult[i++] = array2[b++];
}
}
// tail
if (a < count1)
{
// fill tail from first array
for (int j = a; j < count1; j++)
{
arrayResult[i++] = array1[j];
}
}
else
{
// fill tail from second array
for (int j = b; j < count2; j++)
{
arrayResult[i++] = array2[j];
}
}
// print result
Console.WriteLine("Result is {{ {0} }}", string.Join(",", arrayResult.Select(e => e.ToString())));
结果:
{ 3,5,6,9,12,14,18,20,25,28,30,32,34,36,38,40,42,44,46,48 }
图解说明:
给你一个函数 mergeArrays
,它接受 2 个排序数组作为参数。第一个数组有M
个元素,第二个也有M
个元素,但是容量是2*M
.
函数 mergeArrays
将两个数组作为参数以及 M
。您应该合并第二个数组中的两个数组,以便对结果数组进行排序。
示例测试用例 0:
Input
:
1st array: {3,5,6,9,12,14,18,20,25,28}
2nd array: {30,32,34,36,38,40,42,44,46,48 }
Output: {3,5,6,9,12,14,18,20,25,28,30,32,34,36, 38,40,42,44,46,48}
说明: 如问题中所述,第二个数组包含足够的 space 来容纳第一个数组。 Return 合并后的排序数组。
解决方法可以Linq查询:
int[] first = new int[] { 3, 5, 6, 9, 12, 14, 18, 20, 25, 28 };
int[] second = new int[] { 30, 32, 34, 36, 38, 40, 42, 44, 46, 48 };
int[] result = first
.Concat(second)
.OrderBy(x => x)
.ToArray();
测试
// 3, 5, 6, 9, 12, 14, 18, 20, 25, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48
Console.Write(String.Join(", ", result));
它有效,但很难接受作为家庭作业解决方案。因此,我希望您使用我的实现作为测试参考来详细说明您的自己的代码。
没有linq的解决方案:
int[] array1 = new int[] { 3, 5, 6, 9, 12, 14, 18, 20, 25, 28 };
int[] array2 = new int[] { 30, 32, 34, 36, 38, 40, 42, 44, 46, 48 };
int count1 = array1.Length;
int count2 = array2.Length;
int[] arrayResult = new int[count1 + count2];
int a = 0, b = 0; // indexes in origin arrays
int i = 0; // index in result array
// join
while (a < count1 && b < count2)
{
if (array1[a] <= array2[b])
{
// element in first array at current index 'a'
// is less or equals to element in second array at index 'b'
arrayResult[i++] = array1[a++];
}
else
{
arrayResult[i++] = array2[b++];
}
}
// tail
if (a < count1)
{
// fill tail from first array
for (int j = a; j < count1; j++)
{
arrayResult[i++] = array1[j];
}
}
else
{
// fill tail from second array
for (int j = b; j < count2; j++)
{
arrayResult[i++] = array2[j];
}
}
// print result
Console.WriteLine("Result is {{ {0} }}", string.Join(",", arrayResult.Select(e => e.ToString())));
结果:
{ 3,5,6,9,12,14,18,20,25,28,30,32,34,36,38,40,42,44,46,48 }
图解说明: