title: Bugged MergeSort in Java
body: <p>I'm using a dynamic array using a <code>List</code> and <code>ArrayList</code> classes. I think the bug is the <code>merge</code> method, but I don't what is wrong. I've used the "same" code in JS and Python and everything goes like it is suppose to go.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class mergeS {
  
      private static List&lt;Integer&gt; numbers = new ArrayList&lt;Integer&gt;();
      private static int lon = 8;
  
      public static void main(String[] args) {
          long start, end;
          float time;
          for(int i = 0; i &lt; lon; i++) {
              if(Math.random() &lt; 0.5) {
                  numbers.add((int)(Math.random()*(100)));
              }else {
                  numbers.add((int)(-1*Math.random()*(100)));
              }
          }
          System.out.println("Unsorted list:\n"+numbers+"\n");
          start = System.nanoTime();
          mergeSort(numbers);
          end = System.nanoTime();
          time = (float) ((end-start)/Math.pow(10, 9));
          System.out.println("Sorted list:\n"+numbers);
          System.out.println("The time used was: "+time);
      }
  
      private static void mergeSort(List&lt;Integer&gt; numbers) {
          if (numbers.size() &lt; 2) {
              return;
          }
          int mid = numbers.size() / 2;
          List&lt;Integer&gt; left = numbers.subList(0, mid);
          List&lt;Integer&gt; right = numbers.subList(mid, numbers.size());
          mergeSort(left);
          mergeSort(right);
          merge(left, right, numbers);
      }
  
      private static void merge(List&lt;Integer&gt; left, 
                              List&lt;Integer&gt; right, 
                              List&lt;Integer&gt; numbers) {
          int i = 0, j = 0, k = 0;
          while(i &lt; left.size() &amp;&amp; j &lt; right.size()) {
              if(left.get(i) &lt;= right.get(j)) {
                  numbers.set(k, left.get(i));
                  i++;
              }else {
                  numbers.set(k, right.get(j));
                  j++;
              }
              k++;
          }
          while(i &lt; left.size()) {
              numbers.set(k, left.get(i));
              i++;
              k++;
          }
          while(j &lt; right.size()) {
              numbers.set(k, right.get(j));
              j++;
              k++;
          }
      }
  
  }
  
  </code></pre>