#include<stdio.h> #include<conio.h> void merge(int [],int ,int ,int ); void break_a(int [],int ,int ); int main() { int arr[30]; int i,size; clrscr(); printf("\n\t------- Merge sorting method -------\n\n"); printf("Enter total no. of elements : "); scanf("%d",&size); for(i=0; i<size; i++) { printf("Enter %d element : ",i+1); scanf("%d",&arr[i]); } break_a(arr,0,size-1); printf("\n\t------- Merge sorted elements -------\n\n"); for(i=0; i<size; i++) printf(" %d\n",arr[i]); getch(); return 0; } void break_a(int arr[],int min,int max) { int mid; if(min<max) { mid=(min+max)/2; break_a(arr,min,mid); break_a(arr,mid+1,max); merge(arr,min,mid,max); } } void merge(int arr[],int min,int mid,int max) { int tmp[30]; int i,j,k,m; j=min; m=mid+1; for(i=min; j<=mid && m<=max ; i++) { if(arr[j]<=arr[m]) { tmp[i]=arr[j]; j++; } else { tmp[i]=arr[m]; m++; } } if(j>mid) { for(k=m; k<=max; k++) { tmp[i]=arr[k]; i++; } } else { for(k=j; k<=mid; k++) { tmp[i]=arr[k]; i++; } } for(k=min; k<=max; k++) arr[k]=tmp[k]; }
OUTPUT
------- Merge sorting method -------
Enter total no. of elements : 5
Enter 1 element : 40
Enter 2 element : 10
Enter 3 element : 30
Enter 4 element : 60
Enter 5 element : 20
------- Merge sorted elements -------
10
20
30
40
60