#include< stdio.h>
#include< conio.h>
#define MAX 50
void display(int[MAX],int);
void Merge_sort(int[MAX],int,int);
void Combine(int[MAX],int,int,int);
void main()
{
int i,T[MAX],n;
clrscr();
printf("\n Enter number of element you want:");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("\n Enter element[%d]:",i);
scanf("%d",&T[i]);
}
printf("\n------------------------------------------");
printf("\n Elements before sorting:");
display(T,n);
printf("\n------------------------------------------");
Merge_sort(T,0,n-1);
printf("\n------------------------------------------");
printf("\n Elements after sorting:");
display(T,n);
printf("\n------------------------------------------");
getch();
}
void Merge_sort(int T[MAX],int low,int high)
{
int mid;
if(high > low)
{
mid=(low+high)/2;
Merge_sort(T,low,mid);
Merge_sort(T,mid+1,high);
Combine(T,low,mid,high);
}
}
void Combine(int T[MAX],int low,int mid,int high)
{
int i,j,k,temp[MAX];
k=low;
i=low;
j=mid+1;
while(i <= mid && j <= high)
{
if(T[i] <= T[j])
{
temp[k++]=T[i++];
}
else
{
temp[k++]=T[j++];
}
}
while(i <= mid)
{
temp[k++]=T[i++];
}
while(j <= high)
{
temp[k++]=T[j++];
}
for(i=low;i <= high;i++)
T[i]=temp[i];
}
void display(int T[MAX],int n)
{
int i;
for(i=0;i < n;i++)
{
printf(" %d ",T[i]);
}
}
OUTPUT
Enter number of element you want:7
Enter element[0]:77
Enter element[1]:55
Enter element[2]:44
Enter element[3]:66
Enter element[4]:44
Enter element[5]:22
Enter element[6]:99
------------------------------------------------------------------------------------
Elements before sorting: 77 55 44 66 44 22 99
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
Elements after sorting: 22 44 44 55 66 77 99
------------------------------------------------------------------------------------
Comments
Post a Comment