01 #include<stdio.h>
02 #define MAX 50
03 void mergeSort(int arr[],int low,int mid,int high);
04 void partition(int arr[],int low,int high);
05
06 int main()
07 {
08 int merge[MAX],i,n;
09 printf("Enter the total number of elements: ");
10 scanf("%d",&n);
11 printf("Enter the elements which to be sort: "); for(i=0;i<n;i++)
12 {
13 scanf("%d",&merge[i]);
14 }
15 partition(merge,0,n-1);
16 printf("After merge sorting elements are: ");
17 for(i=0;i<n;i++)
18 {
19 printf("%d ",merge[i]);
20 }
21 return 0;
22 }
23
24 void partition(int arr[],int low,int high)
25 {
26 int mid;
27 if(low<high)
28 {
29 mid=(low+high)/2;
30 partition(arr,low,mid);
31 partition(arr,mid+1,high);
32 mergeSort(arr,low,mid,high);
33 }
34 }
35 void mergeSort(int arr[],int low,int mid,int high)
36 {
37 int i,m,k,l,temp[MAX];
38 l=low;
39 i=low;
40 m=mid+1;
41 while((l<=mid)&&(m<=high))
42 {
43 if(arr[l]<=arr[m])
44 {
45 temp[i]=arr[l];
46 l++;
47 }
48 else
49 {
50 temp[i]=arr[m];
51 m++;
52 }
53 i++;
54 }
55 if(l>mid)
56 {
57 for(k=m;k<=high;k++)
58 {
59 temp[i]=arr[k];
60 i++;
61 }
62 }
63 else
64 {
65 for(k=l;k<=mid;k++)
66 {
67 temp[i]=arr[k];
68 i++;
69 }
70 }
71 for(k=low;k<=high;k++)
72 {
73 arr[k]=temp[k];
74 }
75 }
OUTPUT :
Enter the total number of elements: 5
Enter the elements which to be sort:
1
45
32
12
6
After merge sorting elements are: 1 6 12 32 45