Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 合并两个数组并返回c中的排序数组#_C#_Arrays_Sorting_C# 4.0 - Fatal编程技术网

C# 合并两个数组并返回c中的排序数组#

C# 合并两个数组并返回c中的排序数组#,c#,arrays,sorting,c#-4.0,C#,Arrays,Sorting,C# 4.0,您将获得一个函数mergeArrays,该函数将2个排序数组作为参数。第一个数组中有M个元素,第二个数组也有M个元素,但它的容量是2*M 函数mergeArrays将两个数组与M一起作为参数。您应该将两个数组合并到第二个数组中,以便对结果数组进行排序 示例测试用例0: 输入: 1st数组:{3,5,6,9,12,14,18,20,25,28} 2数组:{30,32,34,36,38,40,42,44,46,48} 输出:{3,5,6,9,12,14,18,20,25,28,30,32,34,36

您将获得一个函数
mergeArrays
,该函数将2个排序数组作为参数。第一个数组中有
M
个元素,第二个数组也有
M
个元素,但它的容量是
2*M

函数
mergeArrays
将两个数组与
M
一起作为参数。您应该将两个数组合并到第二个数组中,以便对结果数组进行排序

示例测试用例0:

输入

1st数组:{3,5,6,9,12,14,18,20,25,28}

2数组:{30,32,34,36,38,40,42,44,46,48}

输出:{3,5,6,9,12,14,18,20,25,28,30,32,34,36,38,40,42,44,46,48}

说明: 如问题中所述,第二个阵列包含足够的空间来容纳第一个阵列。返回合并的排序数组。

解决方案可以是Linq查询:

检验

  // 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())));
图形说明:


听起来像是我的家庭作业。听起来像是家庭作业,也许你应该自己做?创建3d数组,将两个数组合并到其中,然后对3d数组进行排序。你也应该写一篇关于这段代码的解释,以便op可以在他们的文章中包括这一点。我英语说得不好,无法详细解释代码,请留言帮助。对不起,这只是一种讽刺的企图。有时,OP不提供完整的答案更有用,或者像Dmitry这样的答案,它是一个可以很容易理解为伪代码的工作答案,但不是很有用。回答像这样的问题只会导致未来的糟糕问题。我添加了图像表示,这会有所帮助。如果两个数组的大小不同,这将不起作用,一个更简单的解决方案是,检查数组“a”中的最后一个元素是否大于数组“b”中的第一个元素,因为它们的排序条件是小于复制数组“a”在新的一个,比“b”,继续从“a”结束,否则颠倒顺序
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 }