Hi guys I am new here and I need some assitance with my Merge Sort. I have it written but its not working correctly. It just gives me -1, 1.

The way my program works is that the sorts are handled inside the Class Sort so there is no need for passing the array around. Just ignore the excess comments and time functions.


Thanks for any help, and hopefully when I get some free time I can help around here.


bones10925

I don't think your merge sort is completely done yet. Someone tell me if I am wrong (which is very possible), but doesn't MergeSort work more like this... (keep in mind that this way, you are in fact passing arrays around)

void Merge_Sort(int array_MSort[], int* temp, int left, int right)
{
  if (left == right)
    {
      return;
    }
  
  int mid = (left+right)/2;

  Merge_Sort(array_MSort, temp, left, mid);
  Merge_Sort(array_MSort, temp, mid+1, right);
  
  for (int i = left; i <= right; i++)
    {
      temp[i] = array_MSort[i];
    }
  
  int l = left;
  int r = mid + 1;
  
  for(int j=left; j<=right; j++)
    {
      if (l == mid+1)
	{
	  array_MSort[j] = temp[r++];
	}
      
      else if (r > right)
	{
	  array_MSort[j] = temp[l++];
	}
      
      else if(temp[l] < temp[r])
	{
	  array_MSort[j] = temp[l++];
	}
      
      else
	{
	  array_MSort[j] = temp[r++];
	}
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.