MERGE SORT #include # define Null 0 int merge(int *a,int low,int high); int mer(int *a,int low,int high,int mid); int main() { int *a,n,i,flag,k; clrscr(); printf("\nEnter the number of elements:"); scanf("%d",&n); a=(int *)malloc(2*n); if(a==Null) { printf("No memory"); exit(1); } for(i=0;i{ printf("Enter the element:"); scanf("%d",a+i); } merge(a,0,n-1); printf("\nAFTER SORTING\n"); for(i=0;iprintf("%d\t",*(a+i)); getch(); return 1; } int merge(int *a,int low,int high) { int mid; if(low{ mid=(low+high)/2; merge(a,low,mid); merge(a,mid+1,high); mer(a,low,high,mid); } return; } int mer(int *a,int low,int high,int mid) { int i,j,k,*c; 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*(a+i)=*(c+i); return 1; }
SAMPLE INPUT AND OUTPUT:
Enter the number of elements:5 Enter the element:-10 Enter the element:0 Enter the element:-6 Enter the element:5 Enter the element:3
AFTER SORTING -10 -6 0 3 5
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|