-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.c
78 lines (74 loc) · 1.3 KB
/
merge.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
#include <stdio.h>
int a[10], b[10], n;
void print_arr(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
}
void merge(int low, int mid, int high)
{
int i = low;
int j = mid + 1;
int k = low;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
b[k] = a[i];
i++;
k++;
}
else
{
b[k] = a[j];
j++;
k++;
}
}
while (i <= mid)
{
b[k] = a[i];
i++;
k++;
}
while (j <= high)
{
b[k] = a[j];
j++;
k++;
}
for (i = low; i <= high; i++)
{
a[i] = b[i];
}
}
void merge_sort(int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
merge_sort(low, mid);
merge_sort(mid + 1, high);
merge(low, mid, high);
}
}
void main()
{
printf("Enter the no of elements in the array : ");
scanf("%d", &n);
printf("Enter the elements\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The unsorted array is : ");
print_arr(a, n);
int low = 0;
int high = n - 1;
merge_sort(low, high);
printf("The sorted array is : ");
print_arr(a, n);
}