C# 中的归并排序算法问题

发布于 2024-10-08 13:35:17 字数 1569 浏览 0 评论 0原文

这段代码应该像合并排序算法一样工作,但它不起作用并给出输出 0 而不是排序数字,朋友们有什么问题吗?谢谢

 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++;
        }

        while (x > nums.Length){
            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 should work like merge sort algorithm but it doesnt work and gives the output 0 instead of sorting numbers,whats the problem friends?thanks

 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++;
        }

        while (x > nums.Length){
            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技术交流群

发布评论

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

评论(3

不乱于心 2024-10-15 13:35:17

为了完全获得输出,请尝试

   string merge="";
   foreach(var n in nums2)
       merge+=n.ToString() + " ";
   textBox4.Text = merge;

(好吧,使用 Linq & String.Join 或 StringBuilder 可以更快/更好/更漂亮地完成此操作,但出于测试目的,这应该足够了)。

也许这并不能解决上面代码的所有问题,但它可能会帮助您更轻松地调试它。

For getting your output completely, try

   string merge="";
   foreach(var n in nums2)
       merge+=n.ToString() + " ";
   textBox4.Text = merge;

(ok, this can be done faster / nicer / fancier using Linq & String.Join, or a StringBuilder, but for testing purposes this should be enough).

Perhaps this does not solve all the problems with your code above, but it will probably help you to debug it easier.

眉目亦如画i 2024-10-15 13:35:17

该行

if (y > nums1.Length-1)

不应位于内部

if (x > nums1.Length-1)

,因为您要测试每个条件。如果由于 x >= nums.Length - 1 而退出第一个 while 循环,则需要确保已运行 y 到结束也是如此。

The line

if (y > nums1.Length-1)

Should not be inside

if (x > nums1.Length-1)

because you want to test for each of those conditions. If you exit your first while loop because x >= nums.Length - 1, you want to ensure you've run y through to the end as well.

北城孤痞 2024-10-15 13:35:17
  1. 逻辑一团糟。你不应该使用
    'nums2' 来存储结果,我建议你
    应该使用更好的名字。
  2. 您分配 num2 = new int[5] 吗?你
    如果你不这样做,应该使用其他东西
    知道确切的长度。例如使用列表
    反而。性能较慢但其
    更适合小阵列
    排序。
  3. 整数 z = 0;不是正确的方法
    实施。如果你有 while 循环并且
    需要增量计数,为什么不呢
    使用for循环?
  4. 在“if”中,在“while”之后,你保持
    增加 z 而不将其重置为
    '0' 它将索引超出范围

通过 LINQ 可以更轻松地实现逻辑

var numA = new int[]{...};
var numB = new int[]{...};

var result = numA.Union(numB).OrderBy(num => num); // add .Distinct() if you like
  1. the logic is a mess. you shouldn't use
    'nums2' to store result, i suggest you
    should use a better name.
  2. you assign num2 = new int[5] ? you
    should use something else if you don't
    know exact length. eg use List
    instead. Slower performance but its
    more suitable for small array
    sorting.
  3. int z = 0; is not a correct way to
    implement. if you have while loop and
    need increment counting, why don't you
    use for loop?
  4. in 'if' after 'while' you keep
    incrementing z without reset it to
    '0' it will index out of range

The logic is much easier achievable through LINQ

var numA = new int[]{...};
var numB = new int[]{...};

var result = numA.Union(numB).OrderBy(num => num); // add .Distinct() if you like
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文