#include <stdio.h>
#define MAX 100
int a[MAX],temp[MAX];
merge(int low,int mid, int high)
{
int i,j,k;
i=k=low;
j=mid+1;
while(i<= mid && j<=high)
{
while(a[i]<=a[j] && i <=mid)
{
temp[k]=a[i];
k++;
i++;
}
while(a[j]<a[i] && j <= high)
{
temp[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
temp[k]=a[i];
k++;
i++;
}
while(j<=high)
{
temp[k]=a[j];
k++;
j++;
}
for(k=low;k<=high;k++)
{
a[k]=temp[k];
}
}
mergesort(int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
main()
{
int n,i;
printf("enter n value");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
#define MAX 100
int a[MAX],temp[MAX];
merge(int low,int mid, int high)
{
int i,j,k;
i=k=low;
j=mid+1;
while(i<= mid && j<=high)
{
while(a[i]<=a[j] && i <=mid)
{
temp[k]=a[i];
k++;
i++;
}
while(a[j]<a[i] && j <= high)
{
temp[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
temp[k]=a[i];
k++;
i++;
}
while(j<=high)
{
temp[k]=a[j];
k++;
j++;
}
for(k=low;k<=high;k++)
{
a[k]=temp[k];
}
}
mergesort(int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
main()
{
int n,i;
printf("enter n value");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
No comments:
Post a Comment