#include#includevoid merge(int a[], int low, int mid, int high){ int i, m, k ,l,temp[50]; l=low; i=low; m=mid+1; while(l<=mid && m<=high) { if(a[l]<=a[m]) { temp[i]=a[l]; l++; } else { temp[i]=a[m]; m++; } i++; } if(l>mid) { for(k=m;k<=high;k++) { temp[i]=a[k]; i++; } } else { for(k=l;k<=mid;k++) { temp[i]=a[k]; i++; } } for(i=low;i<=high;i++) { a[i]=temp[i]; }} void mergeSort(int a[], int p, int r){ if(p<r) { int q = (p+r)/2; mergeSort(a,p,q); mergeSort(a,q+1,r); merge(a,p,q,r); } }int main(){ int a[]={10, 9, 11, 2, 5, 1},i; int n=sizeof(a)/sizeof(a[0]); mergeSort(a,0,n-1); for(i=0;i< n;i++) printf("%d ", a[i]); getch(); return 0;}
#include
ReplyDelete#include
void merge(int a[], int low, int mid, int high)
{
int i, m, k ,l,temp[50];
l=low;
i=low;
m=mid+1;
while(l<=mid && m<=high)
{
if(a[l]<=a[m])
{
temp[i]=a[l];
l++;
}
else
{
temp[i]=a[m];
m++;
}
i++;
}
if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=a[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
temp[i]=a[k];
i++;
}
}
for(i=low;i<=high;i++)
{
a[i]=temp[i];
}
}
void mergeSort(int a[], int p, int r)
{
if(p<r)
{
int q = (p+r)/2;
mergeSort(a,p,q);
mergeSort(a,q+1,r);
merge(a,p,q,r);
}
}
int main()
{
int a[]={10, 9, 11, 2, 5, 1},i;
int n=sizeof(a)/sizeof(a[0]);
mergeSort(a,0,n-1);
for(i=0;i< n;i++)
printf("%d ", a[i]);
getch();
return 0;
}