Solution:
#include<stdio.h>
void mergeSort(int [],int,int,int);
void partition(int [],int,int);
void main()
{
int A[50],n,i;
printf("Enter size of Array :\n");
scanf("%d",&n);
printf("Enter elements of array :\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
partition(A,0,n-1);
printf("Sorted Array :\n");
for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
}
void partition(int A[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(A,low,mid);
partition(A,mid+1,high);
mergeSort(A,low,mid,high);
}
}
void mergeSort(int A[],int low,int mid,int high)
{
int i,m,k,l,temp[50];
l=i=low;
m=mid+1;
while((l<=mid)&&(m<=high))
{
if(A[l]<=A[m])
{
temp[i]=A[l];
l++;
}
else
{
temp[i]=A[m];
m++;
}
i++;
}
while(l<=mid)
{
temp[i]=A[l];
l++;
i++;
}
while(m<=high)
{
temp[i]=A[m];
i++;
m++;
}
for(k=low;k<=high;k++)
{
A[k]=temp[k];
}
}
0 Comments