enter image description hereThe following is code for the merge sort. I am getting an error on the last line. I have also added a comment. I am unable to retrieve the array returned by the method merge_function(int[] a, int[] b). The error says " non-static method merge_sort(int[],int,int) cannot be referenced from a static context at demo1.Merge.main". Please help..!
public class Merge {
int[] merge_sort(int[] arr, int s, int e){
if(arr.length==1)
return arr;
int m=(s+e)/2;
int []a= merge_sort(arr,s,m);
int []b= merge_sort(arr,m+1,e);
int []c= merge(a,b);
print(arr);
return c;
}
public void print(int[] arr)
{
System.out.println("Elements after sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
int[] merge(int[] a, int[] b)
{
int i=0,j=0,k=0;
int []r=new int[a.length+b.length];
while(i!=a.length && j!=b.length)
{
if(a[i]<b[j])
{
r[k]=a[i];
i++; j++; k++;
}
else if(a[i]==b[j])
{
r[k]=a[i];
i++; j++; k++;
}
else if(a[i]>b[j])
{
r[k]=b[j];
j++; k++;
}
}
return r;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the length of an array.");
int n=in.nextInt();
System.out.println("Enter the numbers.");
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=in.nextInt();
}
//error is here
int []r = merge_sort(arr,0,arr.length-1);
}
}