归并排序算法的输出问题

发布于 2024-10-08 14:11:33 字数 1625 浏览 2 评论 0原文

这段代码给出了输出,但它有一个问题,即当用户在文本框1中写入5,6并在文本框3中写入7,8时,它输出5,6。我知道问题是当数组的元素结束时,它不会打印其余部分其他数组的元素,我对问题行进行了评论。

编辑:我使用textbox1和textbox3来获取用户想要合并的数组的元素

private void button3_Click(object sender, EventArgs e)
{


    string[] source = textBox1.Text.Split(',');
    string[] source1 = textBox3.Text.Split(',');
    int[] nums2 = new int[8];
    int[] nums = new int[source.Length];
    for (int i = 0; i < source.Length; i++)
    {
        nums[i] = Convert.ToInt32(source[i]);

    }
    int[] nums1 = new int[source1.Length];
    for (int j = 0; j < source1.Length; j++)
    {
        nums1[j] = Convert.ToInt32(source1[j]);
    }
    int x = 0;
    int y = 0;
    int z = 0;

    while (x < nums.Length && y < nums1.Length)
    {
        if (nums[x] < nums1[y])
        {
            nums2[z] = nums[x];
            x++;

        }
        else
        {
            nums2[z] = nums1[y];
            y++;
        }

        z++;
    }////----->>it works untill here

    while (x > nums.Length)///this mean when the elements of nums end,out the rest of the elements in other textbox but it doesnt do anything,whats the problem ?
    {
        if (y <= nums1.Length)
        {
            nums2[z] = nums1[y];

            z++;
            y++;
        }
    }
    while (y > nums1.Length)
    {

        if (x <= nums.Length)
        {
            nums2[z] = nums[x];
            z++;
            x++;
        }
    }
        string merge = "";
        foreach (var n in nums2)
            merge += n.ToString() + ",";
        textBox4.Text = merge;


    }

this code gives output but it has one problem that is when user write 5,6 in textbox1 and 7,8 in textbox3 it output 5,6.i know the problem is that when the elements of an array ends,it doesnt print the rest elements of other array,i commented on line of problem.

edited:i used textbox1 and textbox3 for getting the elements of the arrays that user wants to merge

private void button3_Click(object sender, EventArgs e)
{


    string[] source = textBox1.Text.Split(',');
    string[] source1 = textBox3.Text.Split(',');
    int[] nums2 = new int[8];
    int[] nums = new int[source.Length];
    for (int i = 0; i < source.Length; i++)
    {
        nums[i] = Convert.ToInt32(source[i]);

    }
    int[] nums1 = new int[source1.Length];
    for (int j = 0; j < source1.Length; j++)
    {
        nums1[j] = Convert.ToInt32(source1[j]);
    }
    int x = 0;
    int y = 0;
    int z = 0;

    while (x < nums.Length && y < nums1.Length)
    {
        if (nums[x] < nums1[y])
        {
            nums2[z] = nums[x];
            x++;

        }
        else
        {
            nums2[z] = nums1[y];
            y++;
        }

        z++;
    }////----->>it works untill here

    while (x > nums.Length)///this mean when the elements of nums end,out the rest of the elements in other textbox but it doesnt do anything,whats the problem ?
    {
        if (y <= nums1.Length)
        {
            nums2[z] = nums1[y];

            z++;
            y++;
        }
    }
    while (y > nums1.Length)
    {

        if (x <= nums.Length)
        {
            nums2[z] = nums[x];
            z++;
            x++;
        }
    }
        string merge = "";
        foreach (var n in nums2)
            merge += n.ToString() + ",";
        textBox4.Text = merge;


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聊慰 2024-10-15 14:11:33

这样做(删除你的最后一段时间),

while (x < nums.Length)
{
        nums2[z] = nums[x];
        z++;
        x++;
}

while (y < nums1.Length)
{
        nums2[z] = nums1[y];
        z++;
        y++;
}

因为你不知道剩下哪些数组项,而且你当前的代码无论如何也不起作用,因为 y 与 nums 和 vise verse 无关。

编辑:我将第一个 while 复制到第二个 while 中,修复它,删除最后一个 while 循环(其中有 2 个 while )并替换它。

Do (remove your last while)

while (x < nums.Length)
{
        nums2[z] = nums[x];
        z++;
        x++;
}

while (y < nums1.Length)
{
        nums2[z] = nums1[y];
        z++;
        y++;
}

because you are not aware which array items remained, also your current code doesn't work anyway, because y is not related to nums and vise verse.

Edit: I copy past first while into second while, fix it, remove your last while loops (2 while with if in them) and replace this.

獨角戲 2024-10-15 14:11:33

while (x > nums.Length)while (y > nums1.Length) 上的条件都没有意义,因为这永远不会发生。

在前面的块中,只要 xy 小于 nums.Length 或 <代码>nums1.长度。因此,这些永远不会变得更大(最多相等),因此这两个条件将始终为假,并且“剩余”项目不会被合并。

请注意,您的合并排序实现中还有其他问题,但这不在范围内我猜你的具体问题。

Both your conditions on while (x > nums.Length) and while (y > nums1.Length) don't make sense, since this will never happen.

In the block before, you increment x and y as long as they are smaller than nums.Length or nums1.Length. Therefore those will never become larger (at most equal), thus both conditions will always be false and the "remaining" items will not be merged in.

Note that there are other things wrong in your mergesort implementation, but that's not in the scope of your specific question I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文