0

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;


        }
13
  • 3
    One problem is that you haven't aptly named your buttons. Commented Dec 18, 2010 at 8:13
  • 6
    This is a great chance to become familiar with the debugger provided by your IDE. Single-step through the code and watch the values of the variables. Freak out when their values aren't what you expect, and make the appropriate modifications to your code. Commented Dec 18, 2010 at 8:16
  • thanks,but the name of it is correct Commented Dec 18, 2010 at 8:16
  • 6
    Aside from anything else, there is no reason to present this in terms of GUI code. You could very easily write a short but complete program (with predefined data) so we could all run exactly the code you're running. Commented Dec 18, 2010 at 8:17
  • 1
    The line string merge = nums2[z].ToString(); will return you only one single value. Is this what you intended? Commented Dec 18, 2010 at 8:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1
  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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.