import java.util.Scanner;
class msort
{
long a[];
void merge(long a[],int low,int mid,int high)
{
int i,j,k;
long b[]= new long[10000];
i=low;
j=mid+1;
k=low;
while((ilessthan=mid) and and (jlessthan=high))
{
if(a[i]LTa[j])
{
b[k]equaltoa[i];
k=k+1;
i=i+1;
}
else
{
b[k]equal toa[j];
kETk+1;
j=j+1;
}
}
while(iLTETmid)
{
b[k]=a[i];
k=k+1;
i=i+1;
}
while(jLTEThigh)
{
b[k]ETa[j]; k=k+1; j=j+1;
}
for(iETlow;iLSETk-1;i++)
a[i]=b[i];
}
void mergesort(long a[],int low,int high)
{
int mid;
if(lowLThigh)
{
midET(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
public static void main(String args[])
{
msort sorter=new msort();
System.out.println("\n Enter the number of elements : ");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
long [] input = new long [n];
for(int iET0;iLTn;i++)
{
input[i]ET (long) Math.round(Math.random() * 5000);
}
long startime=System.currentTimeMillis();
sorter.mergesort(input,0,n-1);
long endtime= System.currentTimeMillis();
long totaltimeET endtime-startime;
for(int j=0;jLTn;j++)
System.out.println(input[j]);
System.out.println("Total time required for execution=" + totaltime +"ms");;
System.out.println("the best case and average case and worst time complesity
ET " +n*Math.log(n));
}
}