title: Mergesort in java
body: <p>I am new to Java and have tried to implement mergesort in Java. However, even after running the program several times, instead of the desired sorted output, I am getting the same user given input as the output. I would be thankful if someone could help me understand this unexpected behaviour.</p>
  <pre><code>import java.io.*;
  import java.util.Arrays;
  
  public class MergeSort {
  
      public static void main(String[] args) throws IOException {
          BufferedReader R = new BufferedReader(new InputStreamReader(System.in));
          int arraySize = Integer.parseInt(R.readLine());
          int[] inputArray = new int[arraySize];
          for (int i = 0; i &lt; arraySize; i++) {
              inputArray[i] = Integer.parseInt(R.readLine());
          }
          mergeSort(inputArray);
          
          for (int j = 0; j &lt; inputArray.length; j++) {
              System.out.println(inputArray[j]);
          }
      }
      
      static void mergeSort(int[] A) {
          if (A.length &gt; 1) {
              int q = A.length / 2;
              int[] leftArray = Arrays.copyOfRange(A, 0, q);
              int[] rightArray = Arrays.copyOfRange(A, q + 1, A.length);
              mergeSort(leftArray);
              mergeSort(rightArray);
              A = merge(leftArray, rightArray);
          }
      }
      
      static int[] merge(int[] l, int[] r) {
          int totElem = l.length + r.length;
          int[] a = new int[totElem];
          int i, li, ri;
          i = li = ri = 0;
          while (i &lt; totElem) {
              if ((li &lt; l.length) &amp;&amp; (ri &lt; r.length)) {
                  if (l[li] &lt; r[ri]) {
                      a[i] = l[li];
                      i++;
                      li++;
                  } else {
                      a[i] = r[ri];
                      i++;
                      ri++;
                  }
              } else {
                  if (li &gt;= l.length) {
                      while (ri &lt; r.length) {
                          a[i] = r[ri];
                          i++;
                          ri++;
                      }
                  }
                  if (ri &gt;= r.length) {
                      while (li &lt; l.length) {
                          a[i] = l[li];
                          li++;
                          i++;
                      }
                  }
              }
          }
          return a;
      }
  }
  </code></pre>