//Merge Sort...
#include <stdio.h>
void join(int a[], int l, int m, int r)
{
int i, j, k, v[100];
i=l, k=0,j=m+1;
while(i<=m && j<=r)
{
if(a[i]>a[j])
{
v[k]=a[i];
i++;
}
else
{
v[k]=a[j];
j++;
}
k++;
}
while(i<=m)
v[k++]=a[i++];
while(j<=r)
v[k++]=a[j++];
for(i=l;i<=r;i++)
a[i]=v[i-l];
}
void separate(int a[],int l,int r)
{
if(l>=r)
return;
int m=((l+r)/2);
separate(a, l, m);
separate(a, m+1, r);
join(a, l, m, r);
}
int main()
{
int a[100]={5, 3, 9, 10, 2};
int i;
separate(a,0, 4);
for(i=0;i<5;i++)
printf("%d", a[i]);
}
Very informative article.Thank you author for posting this kind of article .
ReplyDeletehttp://www.wikitechy.com/view-article/c-program-to-implement-a-merge-sort-with-example
Both are really good,
Cheers,
Venkat