Skip to content

Instantly share code, notes, and snippets.

@shashirajraja
Created January 31, 2019 19:09
Show Gist options
  • Select an option

  • Save shashirajraja/e01f3b0320209213c46439f39dc9c63c to your computer and use it in GitHub Desktop.

Select an option

Save shashirajraja/e01f3b0320209213c46439f39dc9c63c to your computer and use it in GitHub Desktop.
//MERGE SORT
#include<stdio.h>
#include<conio.h>
int merge(int a[],int lower1,int upper1,int lower2,int upper2)
{
int p,q,j,n,d[100];
p=lower1;
q=lower2;
n=0;
while((p<=upper1) && (q<=upper2))
{
d[n++]=(a[p]<a[q])?a[p++]:a[q++];
}
while(p<=upper1)
d[n++]=a[p++];
while(q<=upper2)
d[n++]=a[q++];
for(q=lower1,n=0;q<=upper1;q++,n++)
{
a[q]=d[n];
}
for(q=lower2,j=n;q<=upper2;q++,j++)
{
a[q]=d[j];
}
return 0;
}
int mergesort(int a[],int lower,int upper)
{
int mid;
if(upper>lower)
{
mid=(lower+upper)/2;
mergesort(a,lower,mid);
mergesort(a,mid+1,upper);
merge(a,lower,mid,mid+1,upper);
}
return 0;
}
int main()
{
int n,a[10],i,k;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("\nSorted array: ");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment