在C#归并排序算法问题算法、问题

由网友(明月何处照归人)分享简介:这code应该像合并排序算法,但它不工作,并给出了输出0,而不是数字进行排序,什么问题的朋友?谢谢私人无效button3_Click(对象发件人,EventArgs的){字符串[]来源= textBox1.Text.Split('');串[] source1中= textBox3.Text.Split(,);INT [...

这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 =合并;
 
C 面试题 四 数据结构常见的八大排序算法

(好吧,这是可以做到更快/更好/发烧友使用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.

阅读全文

相关推荐

最新文章