0% found this document useful (0 votes)
8 views8 pages

Merge Sorting Program

Uploaded by

jonnydada088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Merge Sorting Program

Uploaded by

jonnydada088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MERGE SORTING

INTRODUCTION
• Merge sort is one of the most popular sorting
algorithms that is based on the principle of Divide
and conquer algorithm.

• Here, a problem is divided into multiple sub-


problem. Each sub-problem is solved individually.
finally ,sub-problem are combined to form the final
solution.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define n 6
Void mergesort(int[],int,int);
Void merge(int[],int ,int ,int);
Void main()
{
int x[20],i;
clrscr();
Printf(“Enter the %d elements of the array:\n”);
For(i=0;i<n;i++)
Scanf(“%d”,&x[i]);
Mergesort(x,0,n-1);
Printf(“The sorted array is :”);
For(i=0;i<n;i++)
Printf(“%d->”,x[i]);
getch();
}
//mergesort function

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


{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
//merge funtion
Void merge(int a[],int low,int mid,int high) if(i>mid)
{ {
int b[20],i,j,k; while(j<=high)
i=low; {
j=mid+1; b[k]=a[j];
k=low; j++;
while((i<=mid)&&(j<=high)) k++;
{ }
if(a[i]<=a[j]) }
{ else
b[k]=a[i]; {
i++; while(i<=mid)
k++; {
} b[k]=a[i];
else k++;
{ i++;
b[k]=a[j]; }
j++; }
k++; for(k=low;k<=high;k++)
} a[k]=b[k];
} }
OUTPUT

You might also like