#include<stdio.h>
#include<conio.h>
void merge(int a[50],int,int,int);
void partition(int a[50],int,int);
void main()
{
int a[10],i,n;
clrscr();
printf("Enter the Range:");
scanf("%d",&n);
printf("\n\nEnter the Elements:");
for(i=0 ; i<n ; i++)
{
printf("\n\nEnter Elements %d:",i+1);
scanf("%d",&a[i]);
}
partition(a,0,n-1);
printf("\n\nSorted array:");
for(i=0 ; i<n ; i++)
printf("%d ",a[i]);
getch();
}
void partition(int a[50],int lb,int ub)
{
int mid;
if(lb<ub)
{
mid=(lb+ub)/2;
partition(a,lb,mid);
partition(a,mid+1,ub);
merge(a,lb,mid,ub);
}
}
void merge(int a[50],int lb,int mid,int ub)
{
int temp[50],n=0,p=0,q=0,j=0;
p=lb,q=mid+1;
while((p<=mid)&&(q<=ub))
temp[n++]=(a[p]<a[q]?a[p++]:a[q++]);
while(p<=mid)
temp[n++]=a[p++];
while(q<=ub)
temp[n++]=a[q++];
for(p=lb,n=0;p<=mid;p++,n++)
a[p]=temp[n];
for(p=mid+1,j=n ; p<=ub ; p++,j++)
a[p]=temp[j];
}
0 Comments