title: Eliminating Out of Bounds error in Java sorting comparison
body: <p>I'm currently implementing InsertionSort and MergeSort to do a comparison on their operations, I have implemented MergeSort before but am trying the version in my current algorithms textbook. I am getting an array out of bounds error on the line marked by comments (line 58) and once it gets past it I would assume also probably on line 60. This is the algorithm in the book to the T but I can't find where my error comes from. Code includes simplistic main method to test, full code is included so it is copyable and runnable.</p>
  
  <pre><code>package algsortcomp;
  
  
  public class AlgSortComp {
  
  
  public static void InsertionSort(int[] data){
      //Sorts given array by Insertion Sort
      //Input: an array data of ints
      //Output: array data sorted in nondecreasing 
      int v, j;
      for (int i = 1; i&lt;data.length; i++){
          v = data[i];
          j = i - 1;
          while (j &gt;= 0 &amp;&amp; data[j] &gt; v){
              data[j+1] = data[j--];
          }
          data[j+1]=v;
      }
  }
  
  public static void MergeSort(int[] data){
      //Sorts array data by recursive mergesort
      //Input: an array data of ints
      //Output: array data sorted in nondecreasing order
      int dlen = data.length;
      int mid = dlen/2;
  
      int[] b = new int[mid];
      int[] c = new int[dlen-mid];
  
      if (dlen &gt; 1){
         System.arraycopy(data, 0, b, 0, mid-1);
         System.arraycopy(data, mid, c, 0, dlen-mid);
         MergeSort(b);
         MergeSort(c);
         Merge(b, c, data);
     }         
  }
  
  public static void Merge(int[] b, int[] c, int[] data){
      //Merges two sorted arrays into one sorted array
      //Input: Arrays b and c both sorted
      //Output: Sorted array data of elements b and c.
      int p = b.length, q = c.length, dlen = data.length;
      int i=0, j=0, k=0;
      while (i&lt;p &amp;&amp; j&lt;q){
          if (b[i] &lt;= c[j])
              data[k] = b[i++];
          else
              data[k]=c[j++];
          k++;
      }
      if (i == p)
          System.arraycopy(c, j, data, k, dlen-1); //&lt;---currently getting Array out of bounds here, tried replacing with
      else                                         //dlen-k and error is gone but array returned is all [0,0,...,0]
          System.arraycopy(b, i, data, k, dlen-1);
  }
  
  
  
  public static void main(String[] args) {
      int [] test = {3, 5, 23, 56, 1, 12, 32};
      String arraystr = java.util.Arrays.toString(test);
      System.out.println(arraystr);
  
      //InsertionSort(test);
      MergeSort(test);
      System.out.println(java.util.Arrays.toString(test));
  }
  
  }
  </code></pre>
  
  <p>nobody from class is awake yet so I ask you humble folk for an extra eye to spot my error. Thanks in advance.</p>
  
  <p>edit: also I forget how to call java.util.Arrays.toString() by just Arrays.toString(). I tried importing java.util.Arrays but that didn't work and it has admittedly been quite a while since I've touched java/arrays.</p>