/* Recursive Merge Sort in C Programming */ #include<stdio.h> merge(int a[],int l,int mid,int h) { int i=l,j=mid+1,k=l,c[50]; while((i<=mid)&&(j<=h)) { if(a[i]<=a[j]) c[k++]=a[i++]; else c[k++]=a[j++]; // l=low } // h=high while(i<=mid) c[k++]=a[i++]; while(j<=h) c[k++]=a[j++]; for(i=l;i<k;i++) // necessary loop a[i]=c[i]; } merge_sort(int a[],int l,int h) { int mid; if(l<h) { mid=(l+h)/2; merge_sort(a,l,mid); merge_sort(a,mid+1,h); merge(a,l,mid,h); } } main() { int i,n; printf("\nProgram to sort an array using merge sort : \n"); printf("Enter array capacity : \n"); scanf("%d",&n); int a[n]; for(i=0;i<n;i++) a[i]=rand()%100; printf("Initial array : "); for(i=0;i<n;i++) printf("%4d",a[i]); printf("\n"); merge_sort(a,0,n-1); // merge function printf("\nSorted array : "); for(i=0;i<n;i++) printf("%4d",a[i]); printf("\n"); }
Recursive Merge Sort
Posted by
LAHAUL SETH
~
Recursive Merge Sort
2011-11-29T10:50:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|
Comments (0)
Recursive Merge Sort
2011-11-29T10:50:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|