在C#归并排序算法问题 [英] Problem with mergesort algorithm in C#

查看:144
本文介绍了在C#归并排序算法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code应该像合并排序算法,但它不工作,并给出了输出0,而不是数字进行排序,什么问题的朋友?谢谢

 私人无效button3_Click(对象发件人,EventArgs的)
    {


        字符串[]来源= textBox1.Text.Split('');
        串[] source1中= textBox3.Text.Split(,);
        INT [] nums2 =新INT [8]。
        INT [] = NUM​​S新INT [source.Length]
        的for(int i = 0; I< source.Length;我++)
        {
            NUMS [I] = Convert.ToInt32(来源[I]);

        }
        INT [] nums1 =新INT [source1.Length]
        对于(INT J = 0; J< source1.Length; J ++)
        {
            nums1 [J] = Convert.ToInt32(来源1 [J]);
        }
        INT X = 0;
        INT Y = 0;
        INT Z = 0;

        而(X< nums.Length和放大器;&安培; Y< nums1.Length)
        {
            如果(NUMS [X]< nums1 [Y])
            {
                nums2 [Z] = NUM​​S [X]
                X ++;

            }
            其他
            {
                nums2 [Z] = nums1 [Y]
                ÿ++;
            }

            ž++;
        }

        而(X> nums.Length){
            如果(Y< = nums1.Length)
            {
                nums2 [Z] = nums1 [Y]

                ž++;
                ÿ++;
            }
        }
        而(Y> nums1.Length)
        {

            如果(X< = nums.Length)
            {
                nums2 [Z] = NUM​​S [X]
                ž++;
                X ++;
            }
        }
            序列合并=;
            的foreach(VAR N的nums2)
                合并+ = n.ToString()+,;
            textBox4.Text =合并;


        }
 

解决方案

让你的输出完全,请尝试

 序列合并=;
   的foreach(VAR N的nums2)
       合并+ = n.ToString()+;
   textBox4.Text =合并;
 

(好吧,这是可以做到更快/更好/发烧友使用LINQ和放大器;的string.join或StringBuilder的,但是出于测试目的,这应该是足够了)。

也许这并不能解决所有的问题,你的code以上,但它可能会帮助你调试更方便。

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;


        }

解决方案

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.

这篇关于在C#归并排序算法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆