-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.c
92 lines (71 loc) · 1.06 KB
/
MergeSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdio.h>
void merge(int a[],int temp[],int left,int mid,int right);
void ms(int a[],int temp[],int left,int right)
{
int mid;
if(right>left)
{
mid=(right+left)/2;
ms(a,temp,left,mid);
ms(a,temp,mid+1,right);
merge(a,temp,left,mid+1,right);
}
}
void merge(int a[],int temp[],int left,int mid,int right)
{
int leftend,i,size,temp1;;
leftend=mid-1;
size=right-left+1;
temp1=left;
while(left<=leftend && mid<=right)
{
if(a[left]<=a[mid])
{
temp[temp1]=a[left];
temp1=temp1+1;
left=left+1;
}
else{
temp[temp1]=a[mid];
temp1=temp1+1;
mid=mid+1;
}
}
while(left<=leftend)
{
temp[temp1]=a[left];
temp1=temp1+1;
left=left+1;
}
while(mid<=right)
{
temp[temp1]=a[mid];
temp1=temp1+1;
mid=mid+1;
}
for (i=0;i<size;i++)
{
a[right]=temp[right];;
right=right-1;
}
}
int main()
{
int n;
printf("enter the value of max");
scanf("%d",&n);
int a[n];
int temp[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int left=0;
int right=n-1;
ms(a,temp,left,right);
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}