-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sort.c
More file actions
71 lines (67 loc) · 1020 Bytes
/
Merge_Sort.c
File metadata and controls
71 lines (67 loc) · 1020 Bytes
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
#include<stdio.h>
void merge(int *a, int *b, int low, int pivot, int high)
{
int h,i,j,k;
h=low;
i=low;
j=pivot+1;
while((h<=pivot)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>pivot)
{
for(k=j; k<=high; k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h; k<=pivot; k++)
{
b[i]=a[k];
i++;
}
}
for(k=low; k<=high; k++)
{
a[k]=b[k];
}
}
void mergesort(int *a, int*b, int low, int high) {
int pivot;
if(low<high) {
pivot=(low+high)/2;
mergesort(a,b,low,pivot);
mergesort(a,b,pivot+1,high);
merge(a,b,low,pivot,high);
}
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
int b[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
mergesort(arr,b,0,n-1);
for(int i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
}