CODING:
#include<stdio.h>
#include<conio.h>
void merge(int a[],int,int,int);
void merge_sort(int a[],int,int);
void main()
{
int arry[50],i,n;
clrscr();
printf("How many enter the number:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number:");
scanf("%d",&arry[i]);
}
merge_sort(arry,0,n-1);
printf("\nSorted The List.");
for(i=0;i<n;i++)
printf("\n%d",arry[i]);
getch();
}
void merge_sort(int arry[],int beg,int end)
{
int i,mid;
if(beg<end)
{
mid=(beg+end)/2;
merge_sort(arry,beg,mid);
merge_sort(arry,mid+1,end);
merge(arry,beg,mid,end);
}
}
void merge(int arry[],int beg,int mid,int end)
{
int i=beg,j=mid+1,ii=beg,temp[50],k;
while((i<=mid) && (j<=end))
{
if(arry[i]<arry[j])
{
temp[ii]=arry[i];
i++;
}
else
{
temp[ii]=arry[j];
j++;
}
ii++;
}
if(i>mid)
{
while(j<=end)
{
temp[ii]=arry[j];
j++;
ii++;
}
}
else
{
while(i<=mid)
{
temp[ii]=arry[i];
i++;
ii++;
}
}
for(k=beg;k<ii;k++)
arry[k]=temp[k];
}
#include<stdio.h>
#include<conio.h>
void merge(int a[],int,int,int);
void merge_sort(int a[],int,int);
void main()
{
int arry[50],i,n;
clrscr();
printf("How many enter the number:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number:");
scanf("%d",&arry[i]);
}
merge_sort(arry,0,n-1);
printf("\nSorted The List.");
for(i=0;i<n;i++)
printf("\n%d",arry[i]);
getch();
}
void merge_sort(int arry[],int beg,int end)
{
int i,mid;
if(beg<end)
{
mid=(beg+end)/2;
merge_sort(arry,beg,mid);
merge_sort(arry,mid+1,end);
merge(arry,beg,mid,end);
}
}
void merge(int arry[],int beg,int mid,int end)
{
int i=beg,j=mid+1,ii=beg,temp[50],k;
while((i<=mid) && (j<=end))
{
if(arry[i]<arry[j])
{
temp[ii]=arry[i];
i++;
}
else
{
temp[ii]=arry[j];
j++;
}
ii++;
}
if(i>mid)
{
while(j<=end)
{
temp[ii]=arry[j];
j++;
ii++;
}
}
else
{
while(i<=mid)
{
temp[ii]=arry[i];
i++;
ii++;
}
}
for(k=beg;k<ii;k++)
arry[k]=temp[k];
}
Comments
Post a Comment