26

For a small homework assignment, I'm supposed to write a simple merge function whose prototype looks like this:

void merge(int a[], int left_low,  int left_high, int right_low, int right_high)

The directions say that for simplicity's sake, we are only taking in a single array, a[] and that right_low = left_high + 1. We are also storing the final values in the original array a[] that was passed in. Essentially, for an array with values a[] = {1,3,10,4,7,8} it looks like this:

a = {1, 3,     10 ,         4,    7,      8}
     ^         ^            ^             ^ 
  left_low  left_high    right_low     right_high

For this assignment, we have a few tests we have to pass. The first one is a simple merge between two arrays. The second is the teachers own merge_sort function that he calls on some randomly sorted arrays. Here is my implementation of merge():

void merge(int a[], int left_low,  int left_high,
                    int right_low, int right_high) {
    int temp[right_high + 1]; // temporary array to store the result
    int left_i = left_low, right_i = right_low, temp_i = 0;

    // while the temporary array is not filled
    while(temp_i != right_high + 1)
    {
        if(left_i == left_high + 1)
            temp[temp_i++] = a[right_i++];
        else if(right_i == right_high + 1)
            temp[temp_i++] = a[left_i++];
        else if(a[left_i] < a[right_i])
            temp[temp_i++] = a[left_i++];
        else
            temp[temp_i++] = a[right_i++];
    } // end while
    for(int i = 0; i < temp_i; ++i)
        a[i] = temp[i];
}

When he calls the first test, where he just checks the merging of two arrays, my function works, and the single array is now sorted. However, when he calls his merge_sort function, I end up getting garbage values. Here are his test functions:

template<class T>
void print (std::string label, T a[], int length, bool report_sorted) {
  bool sorted = true;
  std::cout << label;
  for (int i=0; i<length; ++i) {
    std::cout << a[i];
    if (i == length-1)
      std::cout << std::endl;
    else {
      std::cout << ", ";
      if (a[i] > a[i+1])
        sorted = false;
    }
  }
  if (report_sorted)
    std::cout << (sorted ? "    Sorted" : "    Not Sorted") << std::endl;
}

void shuffle(int values[], int length) {
  std::vector<int> v_values;
  for (int i=0; i<length; ++i)
    v_values.push_back(values[i]);
  std::random_shuffle(v_values.begin(),v_values.end());
  for (int i=0; i<length; ++i)
    values[i] = v_values[i];
}

//Recursive Merge Sort
template<class T>
void merge_sort(T a[], int low, int high) {
  if (high - low < 1)               //Base case: 0 or 1 value to sort -> sorted
    return;
  else {
    int mid = (low + high)/2;       //Split in 1/2
    merge_sort(a, low, mid);        //Recursively sort low to mid
    merge_sort(a, mid+1, high);     //Recursively sort mid+1 to high
    merge(a, low,mid, mid+1,high);  //Merge sorted parts of array
  }
}

//Standard Merge Sort (calls a generalized one, with more parameters)
template<class T>
void merge_sort(T a[], int length) {
  merge_sort(a, 0, length-1);
}

std::cout << "\n\nTesting merge in merge sort" << std::endl;
    int test_merge_sort[10] = {1,2,3,4,5,6,7,8,9,10};
    for (int i=0; i<5; i++) {
      shuffle(test_merge_sort, 10);
      print("\n  Array before sort: ", test_merge_sort, 10, false);
      merge_sort(test_merge_sort, 10);
      print("  Array after  sort: ", test_merge_sort, 10, true);
    }

And for some reason, my output ends up looking like this:

 Array before sort: 3, 9, 2, 5, 8, 4, 6, 10, 1, 7
  Array after  sort: -944749486, 4, 5439488, 0, 5443192, 5443196, 1975317641, -944749486, 4, 1995111146
    Not Sorted

  Array before sort: 1995111146, 1975317641, 4, 0, -944749486, 5443192, 5443196, 5439488, 4, -944749486
  Array after  sort: -944749486, 4, 5439488, 0, 5443192, 5443196, 1975317641, -944749486, 4, 1995111146
    Not Sorted

  Array before sort: -944749486, -944749486, 5443196, 4, 5439488, 1995111146, 5443192, 1975317641, 0, 4
  Array after  sort: -944749486, 4, 5439488, 0, 5443192, 5443196, 1975317641, -944749486, 4, 1995111146
    Not Sorted

  Array before sort: 1975317641, -944749486, 4, 4, 5439488, 5443192, 5443196, -944749486, 0, 1995111146
  Array after  sort: -944749486, 4, 5439488, 0, 5443192, 5443196, 1975317641, -944749486, 4, 1995111146
    Not Sorted

  Array before sort: -944749486, 5443192, 5443196, 1975317641, 4, 0, -944749486, 5439488, 1995111146, 4
  Array after  sort: -944749486, 4, 5439488, 0, 5443192, 5443196, 1975317641, -944749486, 4, 1995111146
    Not Sorted

What is going wrong with my merge code that could be causing this?

3
  • 4
    Did you really get this as an assignment from a teacher? The point is that int a[] is very misleading, it doesn't pass an array to the function but is equivalent to int* a, i.e. a simple pointer, which also means that modifying the content will cause changes to the caller's data. Feb 22, 2015 at 10:13
  • 16
    @UlrichEckhardt I didn't know it was actually passing in a pointer.. that makes a lot more sense now. And yep, it's a real assignment. The teacher has taught for a very long time, but really only in Java. A couple weeks before the quarter started he posted on his site saying he had "just learned C++ on week long cruise, but don't worry, everything pretty much translates from Java so it's not too bad." That statement pretty much sums up the course.
    – Alex
    Feb 22, 2015 at 10:28
  • 2
    @Alex: Yes, he's spot on: "One can program FORTRAN in any language"... and my sympathy. Feb 24, 2015 at 22:50

1 Answer 1

16

The problem is that you are computing the number of entries in temp incorrectly: your code thinks it is right_high + 1, but the correct formula is right_high - left_low + 1.

For example, when a call gives you indexes 10, 15, 16, 26, your code tries to merge 27 values, while it should be merging only 17 (i.e. indexes 10 through 26, inclusive).

This makes no difference when left_low is zero, so your test case runs fine. But as soon as left_low becomes nonzero, e.g. when the right half of the array is being sorted, your code "overshoots" both arrays, placing garbage values into tmp and writing over values in the array a as well.

Also the assignments in the last for loop need to be at offset left_low as well:

for(int i = 0; i < temp_i; ++i)
    a[i+left_low] = temp[i];
1
  • This does make sense, but for some reason my array now slowly outputs weird values throughout the loop. It will sort the first thing, but then do something like this: Array after sort: 4, 4, 6, 7, 4, 6, 7, 7, 7, 4, Array after sort: 4, 4, 6, 4, 6, 4, 7, 4, 7, 4
    – Alex
    Feb 21, 2015 at 1:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.