//Merge Sort algorithm implementation.
#include<stdio.h>
#include<conio.h>
int z=1;
void merge(int a[], int low, int high, int mid)
{
printf("\nStep %d:\t",z++);
int i,j,k,c[50];
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
{
if(a[i]<a[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
c[k]=a[i];
k++;
i++;
}
while(j<=high)
{
c[k]=a[j];
k++;
j++;
}
for(i=low;i<k;i++)
{
a[i]=c[i];
printf("%d\t",a[i]);
}
}
void merge_sort(int a[], int low, int high)
{
int mid;
printf("\n High: %d, Low: %d, mid: %d\n\n",high,low,((high+low)/2));
if(low<high)
{
mid=(low+high)/2;
merge_sort(a, low, mid);
merge_sort(a, mid+1, high);
merge(a, low, high, mid);
}
}
void main()
{
int a[10],n;
clrscr();
printf("Enter No. of Items to insert: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
merge_sort(a,0,n-1);
getch();
}
//program written by Mars.
.thank you for sharing useful post.
ReplyDeletec++ programming tutorial
welookups