Thursday, September 22, 2011

IMPLEMENTATION OF MERGE SORT

IMPLEMENTATION OF MERGE SORT

PROGRAM:

#include
#include
#include
int n;
void main()
{
int i,low,high;
int a[20];
void mergesort(int a[10],int low,int high);
void display(int a[10]);
clrscr();
printf("\n\tMerge Sort");
printf("\n Enter the size of elements");
scanf("%d",&n);
printf("\n Enter the list element");
for(i=0;iscanf("%d",&a[i]);
low=0;
high=n-1;
mergesort(a,low,high);
display(a);
getch();
}
void mergesort(int a[10],int low,int high)
{
int mid;
void combine(int a[10],int low,int mid,int high);
if(low{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
combine(a,low,mid,high);
}
}
void combine(int a[10],int low,int mid,int high)
{
int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while(i<=mid&&j<=high)
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=high)
{
temp[k]=a[j];
j++;
k++;
}
for(k=low;k<=high;k++)
a[k]=temp[k];
}
void display(int a[10])
{
int i;
printf("\n\n The Sorted Array is.......");
for(i=0;iprintf("%d\t",a[i]);
}









OUTPUT:

Merge Sort
Enter the size of elements 5

Enter the list element
9
8
7
6
5


The Sorted Array is.......5 6 7 8 9




CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )