Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views1 page

Merge

The document contains a C program that implements the merge sort algorithm to sort an array of integers. It prompts the user to input the number of elements and the elements themselves, displays the array before and after sorting, and utilizes a recursive approach to sort the array. The program includes functions for both the merge sort and the merging process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Merge

The document contains a C program that implements the merge sort algorithm to sort an array of integers. It prompts the user to input the number of elements and the elements themselves, displays the array before and after sorting, and utilizes a recursive approach to sort the array. The program includes functions for both the merge sort and the merging process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
#include<conio.h>
void main()
{
int a[20],n,i;
void mergesort(int[],int,int);
clrscr();
printf("\nEnter n:");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nBefore sorting:");
for(i=0;i<n;i++)
printf("%4d",a[i]);
mergesort(a,0,n-1);
printf("\nAfter sorting:");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
}

void mergesort(int a[],int low,int high)


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

void merge(int a[],int low,int mid,int high)


{
int i,j,k,c[20];
i=low;
j=mid+1;
for(k=low;i<=mid && j<=high;k++)
{
if(a[i]<a[j])
c[k]=a[i++];
else
c[k]=a[j++];
}
while(i<=mid)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=c[i];
}

You might also like