title: Merge Sort Example
body: <p>I am new to Java and I have tried writing a Merge Sort Program .
  MergeSort.java program is below :-</p>
  
  <pre><code>public class MergeSort {
  
      public static int b[] = {6,2,3,1,9};
  
      public void MergeSort(int a[],int left[], int right[]) {
  
          int i = 0,j = 0,k = 0;
  
          int l = left.length;
  
          int r = right.length;
          int v = a.length;
  
          while ( i &lt; l &amp;&amp; j &lt; r) {
              if( left[i] &lt; right[j]) {
                  a[k] = left[i] ;
                  i++;
              } else {
                  a[k] = right [j];
                  j++;
              }
              k++;
          }
          while ( i &lt; l ) {
              a[k] = left[i];
              k++;
              i++;
  
          }
          while ( j &lt; r ) {
              a[k] = right[j];
              k++;
              j++;
  
          }
  
      }
  
      public void Merge(int a[], int length) {
  
  
          int n = length;
          if(n &lt; 2) return;
          int mid = n/2;
  
          int left[] = new int[mid];
          int right[] = new int[n-mid];
          for (int i = 0 ; i &lt;mid ; i++) {
              left[i] = a[i];
          }
          for (int i = mid ; i &lt;n ; i++) {
              right[i-mid] = a[i];
          }
  
          Merge(right,n-mid);
          Merge(left,mid);
          MergeSort(b, left, right);
  
      }
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
  
          MergeSort ms = new MergeSort();
          ms.Merge(b,b.length);
          for(int i=0 ; i &lt; b.length ; i++)
          System.out.println(b[i]);
  
      }
  
  }
  </code></pre>
  
  <p>When I run this program , the output I get is </p>
  
  <pre><code>3
  1
  6
  2
  9
  </code></pre>
  
  <p>But , the expected output is <code>1 2 3 6 9</code></p>
  
  <p>Can anyone please help me point out the mistake I am doing and try to help me fix it please .I have tried debugging it on Eclipse but could not spot the bug .
  Thanks </p>