Q:

# C Program For MERGE Sort In Ascending Order

Like QuickSort  Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then it merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one

``````#include
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main()
{
int merge[MAX],i,n;

printf("Enter the total number of elements: ");
scanf("%d",&n);

printf("Enter the elements which to be sort: ");
for(i=0;i         scanf("%d",&merge[i]);
}

partition(merge,0,n-1);

printf("After merge sorting elements are: ");
for(i=0;i         printf("%d ",merge[i]);
}

return 0;
}

void partition(int arr[],int low,int high)
{

int mid;

if(low         mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void mergeSort(int arr[],int low,int mid,int high)
{

int i,m,k,l,temp[MAX];

l=low;
i=low;
m=mid+1;

while((l<=mid)&&(m<=high))
{

if(arr[l]<=arr[m])
{
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}

if(l>mid){
for(k=m;k<=high;k++)
{
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++)
{
temp[i]=arr[k];
i++;
}
}

for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}``````

Output:

Enter the total number of elements :7

Enter the elements which to be sort: -569

-2

0

322

545

784

4584

After merge sorting elements are : -569 -2 0 322 545 784 4584