title: Merge Sort Not Working?
body: <p>I'm having some trouble getting my Merge Sort function to work with the specifications given to me by my professor. Been staring at VS and Google for an eternity trying to figure this guy out.</p>
  
  <p>The algorithm provided:</p>
  
  <p><a href="https://i.stack.imgur.com/knrlo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/knrlo.png" alt="enter image description here"></a></p>
  
  <p>arrayFunctions.h</p>
  
  <pre><code>template&lt;class T&gt;
  void printArray(T arr[], int numElements)
  {
      cout &lt;&lt; "(";
  
      for (int i = 0; i &lt; numElements; i++)
      {
  
          cout &lt;&lt; arr[i];
  
          if (i &lt; numElements - 1)
              cout &lt;&lt; ", ";
      }
  
      cout &lt;&lt; ")" &lt;&lt; "\n\n";
  }
  
  template &lt;class T&gt;
  void setArray(T to[], T from[], int size)
  {
      for (int i = 0; i &lt; size; i++)
          to[i] = from[i];
  }
  
  template &lt;class T&gt;
  void setArray(T to[], T from[], int size1, int size2)
  {
      int size = size1;
      if (size2 &lt; size1) size = size2;
  
      setArray(to, from, size);
  }
  </code></pre>
  
  <p>main</p>
  
  <pre><code>const int NUM = 5;
  
  int originalArray[NUM] = { 4, 2, 5, 3, 1 };
  int newArray[NUM];
  
  cout &lt;&lt; "Original:\n";
  printArray(originalArray, NUM); //prints an array with formatting
  
  // Merge Sort
  setArray(newArray, originalArray, NUM); //set's newArray to the same values of originalArray
  mergeSort(newArray, 0, NUM - 1);
  
  cout &lt;&lt; "Merge Sort:\n";
  printArray(newArray, NUM);
  
  pause();
  </code></pre>
  
  <p>The output when running main is:</p>
  
  <p>Original:
  (4, 2, 5, 3, 1 )</p>
  
  <p>Merge Sort:
  (0, 0, 0, -33686019, 1)</p>
  
  <p>merge:</p>
  
  <pre><code>template &lt;class T&gt;
  void merge(T L[], int lowerBound, int mid, int upperBound)
  {
      // Get size for new arrays
      int size1 = mid - lowerBound;
      int size2 = upperBound - mid;
  
      // Create Temporary Arrays
      T * tmp1 = new T[size1 + 1]();
      T * tmp2 = new T[size2 + 1]();
  
  
      // Populate both arrays from original
      for (int i = 0; i &lt; size1; i++)
          tmp1[i] = L[lowerBound + i];
  
      for (int j = 0; j &lt; size2; j++)
          tmp2[j] = L[mid + j];
  
      tmp1[size1] = numeric_limits&lt;T&gt;::max();
      tmp2[size2] = numeric_limits&lt;T&gt;::max();
  
      int i = 0;
      int j = i;
  
      for (int k = lowerBound; k &lt; upperBound; k++)
      {
          if (tmp1[i] &lt;= tmp2[j])
          {
              L[k] = tmp1[i];
              i++;
          }
          else
          {
              L[k] = tmp2[j];
              j++;
          }
      }
  
      delete[] tmp1;
      delete[] tmp2;
  }
  </code></pre>
  
  <p>mergeSort:</p>
  
  <pre><code>template&lt;class T&gt;
  void mergeSort(T L[], int lowerBound, int upperBound)
  {
      if (lowerBound &lt; upperBound)
      {
          int mid = (lowerBound + upperBound) / 2;
  
          mergeSort(L, lowerBound, mid);
          mergeSort(L, mid + 1, upperBound);
  
          merge(L, lowerBound, mid, upperBound);
      }
  }
  </code></pre>
  
  <p>So... what am I doing wrong? A bump in the right direction would be GREATLY appreciated.</p>