0

I am currently triyng to sort some intergers with Merge Sort, but something is wrong with my algoritm. I have a larger file with integers Im supposed to sort, but I use a smaller, given array to check if its working before sorting the larger file.

My Output from THIS algoritm is: 1 2 2 2 4 5 6, but it's supposed to be: 1 2 4 5 6 9 10 ??

Here's what I've got:

private static int[] data = new int[] { 1, 9, 10, 2, 4, 5, 6 };

static void Main(string[] args)
{
   int N = data.Length;
   Sort(data, 0, N - 1);
   for (int i = 0; i < N; i++)
       Console.WriteLine(data[i]);
}

private static void Merge(int[] intArray, int lo, int mid, int hi)
{
   int i = lo;
   int j = mid + 1;
   if (intArray.Length != 0)
       for (int k = lo; k <= hi; k++)
           data[k] = intArray[k];
   for (int k = lo; k <= hi; k++)
   {
       if (i > mid)
           intArray[k] = data[j++];
       else if (j > hi)
           intArray[k] = data[i++];
       else if (data[j] < data[i])
           intArray[k] = data[j++];
       else if (data[i] < data[j])
           intArray[k] = data[i++];
   }
}

private static void Sort(int[] intArray, int lo, int hi)
{
   if (hi <= lo)
       return;
   int mid = lo + (hi - lo) / 2;
   Sort(intArray, lo, mid);
   Sort(intArray, mid + 1, hi);
   Merge(intArray, lo, mid, hi);
} 
2
  • 3
    data[k] = intArray[k]; -- data and intArray are the same array, so this line does nothing (it's exactly the same as data[k] = data[k], because intArray points to the same array as data. I suspect your problems come from this misunderstanding of how C# reference types work. Commented Oct 4, 2019 at 11:31
  • Thank you for your reply! The only thing I want the achieve in that part, is that if there's only 1 or less elements in the array, theres nothing more to sort. Then it should be the very same array. If i comment out that whole if statement with the for-loop, the output is still the same. Commented Oct 4, 2019 at 11:46

1 Answer 1

2

I think the problem is in array reference in C# as @canton7 said, try this code:

private static int[] data = new int[] { 1, 9, 10, 2, 4, 5, 6 };
private static int[] intArray;

static void Main()
{
    int N = data.Length;
    intArray = new int[N];
    Sort(0, N - 1);
    for (int i = 0; i < N; i++)
        Console.WriteLine(intArray[i]);
}

private static void Merge(int lo, int mid, int hi)
{
    int i = lo;
    int j = mid + 1;      
    for (int k = lo; k <= hi; k++)
    {
        if (i > mid)
            intArray[k] = data[j++];
        else if (j > hi)
            intArray[k] = data[i++];
        else if (data[j] < data[i])
            intArray[k] = data[j++];
        else if (data[i] < data[j])
            intArray[k] = data[i++];
    }
    if (intArray.Length != 0)
        for (int k = lo; k <= hi; k++)
            data[k] = intArray[k];
}

private static void Sort(int lo, int hi)
{
    if (hi == lo)
        return;
    int mid = lo + (hi - lo) / 2;
    Sort(lo, mid);
    Sort(mid + 1, hi);
    Merge(lo, mid, hi);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! That helped me a lot. It is now working perfectly. Really appreciate it!
You are welcome, just avoid dealing with arrays in C# as they will be copied between methods. This doesn't happed unfortunately.

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.