MERGE SORT
#include<stdio.h>
void mergeSort(int arr[ ],int low, int mid, int
high);
void partition(int arr[ ],int low, int high);
void main()
{
int a[100],
i, n;
clrscr();
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
partition(a,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",merge[i]);
}
}
void partition(int a[ ], int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(a, low, mid);
partition(a, mid+1, high);
mergeSort(a, low, mid, high);
}
}
void mergeSort(int arr[ ], int low, int mid, int
high)
{
int i, m, k,
l, temp[100];
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];
}
}
0 comments:
Post a Comment