Recursive Merge Sort in Java

import java.io.*;
public class merge
{
    void merge(int a[],int l,int mid,int h)
       {   
        int i=l,j=mid+1,k=l;
        int c[]=new int[50];
           
        while((i<=mid)&&(j<=h))
            {
                if(a[i]<=a[j])
                    c[k++]=a[i++];
                else
                    c[k++]=a[j++];        // l=low                  
            }                    // h=high
        while(i<=mid)           
            c[k++]=a[i++];               
           
        while(j<=h)
            c[k++]=a[j++];               
       
        for(i=l;i<k;i++)    // necessary loop
            a[i]=c[i];                   
       }
    void merge_sort(int a[],int l,int h)
       {
        int mid;
        if(l<h)
            {    mid=(l+h)/2;
                merge_sort(a,l,mid);
                merge_sort(a,mid+1,h);
                merge(a,l,mid,h);
            }
     } 
    public void main(String args[])throws IOException
{
    System.out.print("\nProgram to sort array using merge sort : \n");
    int n,i;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("\nEnter array capacity : ");
    n=Integer.parseInt(br.readLine());
    int a[]=new int[n];
    for(i=0;i<n;i++)
        a[i]=(int)(Math.random()*100);          // Generates random numbers
   System.out.print("\nArray before sorting : ");
    for(i=0;i<n;i++)
        System.out.print(a[i] + "   ");
        merge_sort(a,0,n-1);
    System.out.print("\nArray after sorting : ");
    for(i=0;i<n;i++)
        System.out.print(a[i] + "   ");

    }
   
}

Top