import [Link].
*;
public class quic
{
static final int MAX=100001;
static final int[] a=new int[MAX];
public static void main(String[] args)
{
[Link]("Enter the number of elements in the array : ");
Scanner sc=new Scanner([Link]);
int n=[Link]();
Random ran= new Random();
for(int i=0;i<n;i++)
{
a[i]=[Link](100);
}
[Link]("The actual elements of the array are : ");
for(int i=0;i<n;i++)
{
[Link](a[i]+" ");
}
[Link]();
int[] quicksrt=[Link](a, n);
long quickstrt =[Link]();
quicksort(0,n-1,quicksrt);
long quickstp =[Link]();
long eltime=(quickstp-quickstrt);
[Link]("The elapsed time for quick sort is :"+
(double)eltime/1000000+ "ms");
[Link]("The sorted elements are : ");
for(int i=0;i<n;i++)
{
[Link](quicksrt[i]+" ");
}
[Link]();
int[] mergsrtarr=[Link](a, n);
[Link]("The actual elements of the array before merge
sort is :");
for(int i=0;i<n;i++)
{
[Link](mergsrtarr[i]+" ");
}
[Link]();
long mergestrTime=[Link]();
Mergesortalgo(0,n-1,mergsrtarr);
long mergestpTime=[Link]();
long Elapsedtime=(mergestpTime-mergestrTime);
[Link]("The total elapsed time for the merge sort is :
"+(double)Elapsedtime/1000000);
[Link]("The merge sorted array is the :");
for(int i=0;i<n;i++)
{
[Link](mergsrtarr[i] + " ");
}
}
public static void quicksort(int p,int r,int[] a)
{
int i,j,temp,pivot;
if(p<r)
{
i=p;
j=r;
pivot= a[p];
while(true)
{
i++;
while(a[i]<pivot && i<r)
{
i++;
}
while(a[j]>pivot)
{
j--;
}
if(i<j)
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
else
{
break;
}
a[p]=a[j];
a[j]=pivot;
quicksort(p, j-1,a);
quicksort(j+1, r,a);
}
}
public static void Mergesortalgo(int low,int high,int[] a)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
Mergesortalgo(low, mid, a);
Mergesortalgo(mid+1, high, a);
merge(a,low,mid,high);
}
}
public static void merge(int[] a,int low,int mid,int high)
{
int[] b=new int[MAX];
int h,i,j,k;
i=h=low;
j=mid+1;
while((h<=mid)&&(j<=high))
if(a[h]<a[j])
b[i++]=a[h++];
else
b[i++]=a[j++];
if(h>mid)
for( k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
for(k=low;k<=high;k++)
a[k]=b[k];
}
}