#include<stdio.h>
#include<conio.h>
void partition(int [],int,int);
void merge_sort(int [],int,int,int);
void main()
{
int a[10],i,n;
clrscr();
printf("\nEnter no of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter a[%d]",i);
scanf("%d",&a[i]);
}
partition(a,0,n-1);
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}
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);
merge_sort(a,low,mid,high);
}
}
void merge_sort(int a[],int low,int mid,int high)
{
int temp[10];
int i,j,k,m;
m=mid+1;
for(i=low;j<=mid && m<=high;i++)
{
if(a[j]<=a[m])
{
temp[i]=a[j];
j++;
}
else
{
temp[i]=a[m];
m++;
}
}
if(j>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=a[k];
i++;
}
}
else
{
for(k=j;k<=mid;k++)
{
temp[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)
{
a[k]=temp[k];
}
}
No comments:
Post a Comment