# Merge Sort Program in C in C

/*
Merge Sort, Written in C,
Author: Juneja Afzal, T.E, computer engg
*/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int a[50],n,low,high,mid,i;
void mergesort(int a[50] ,int ,int);
//                          void combine(int,int ,int);
void display(int a[50],int);
printf("\nENter the number of element for arrra===");
scanf("%d",&n);
printf("\n*******Enter the Elements****\n");
for(i=0;i<=n-1;i++)
{
printf("\nENter the element number %d=== ",i+1);
scanf("%d",&a[i]);
}

low=0;
high=n-1;
//
mergesort(a,low,high);
display(a,n);
}

void mergesort(int a[], int low, int high)
{
int mid;
void combine(int a[], int, int, int);
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
combine(a,low,mid,high);
}
}

void combine(int a[], int low, int mid, int high)
{
int i,j,k,temp[50];
i=low;
k=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[],int n)
{
int i=0;
printf("\n*********THE SORTED LIST IS USING MERGE SORT*********\n");
for(i=0;i<=n-1;i++)
{
printf("\n \t %d",a[i]);
}
}