title: Implement recursive MergerSort in Java
body: <p>I have a problem implementing merge-sort in Java. The objective is to have two classes, the first one is union, which combines two already sorted arrays (I think I got that one right). The other class, "sort" however is where I'm really stuck. I want to split an array recursively into smaller arrays and then combine them with union. Here's what I got so far, I hope there is someone who can help me... Thanks!</p>
  
  <pre><code>class MergeSort {
   public static int[] sort(int[] array) {
  
  int[] firsthalf = new int[array.length];  #muss noch verschoben werden, a
  System.arraycopy(array, 0, firsthalf, 0, array.length);
  
  int[] firsthalf = Arrays.copyofRange(firsthalf, 0, lefthalf.length/2)
  int[] secondhalf= Arrays.copyofRange(
  public static int[] union(int[] sortedIntArray1,
                          int[] sortedIntArray2) {
  
  
  
  int[] neuesarray = new int[sortedIntArray1.length + SortedIntArray2.length];
  int i = 0, j = 0, k = 0;
  while (i &lt; sortedIntArray1.length &amp;&amp; j &lt; sortedIntArray2.length)
  {
      if (sortedIntArray1[i] &lt; sortedIntArray2[j]) #wenn liste an stelle i kleiner als liste 2 an stelle j 
      {
          neuesarray[k] = sortedIntArray1[i]; 
          i++;
      }
      else
      {
          answer[k] = sortedIntArray2[j];                       #bei umgekehrtem fall, wird von liste2 das element angehängt
          j++;
      }
      k++;
  }
  
  while (i &lt; sortedIntArray1.length)          #diese while schleifen sind wichtig, sonst bleibt der Zeiger praktisch stehen
  {
      neuesarray[k] = sortedIntArray1[i];
      i++;
      k++;
  }
  
  while (j &lt; sortedIntArray2.length)
  {
      neuesarray[k] = sortedIntArray2[j];
      j++;
      k++;
  }
  
  return neusarray;
  } 
  }
  }
  </code></pre>