-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathHacktoberfest'22_PR1
68 lines (57 loc) · 1.27 KB
/
Hacktoberfest'22_PR1
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
#include <stdio.h>
void merge_sort(int i, int j, int a[], int aux[])
{
if (j <= i)
{
return;
}
int mid = (i + j) / 2;
merge_sort(i, mid, a, aux);
merge_sort(mid + 1, j, a, aux);
int pointer_left = i;
int pointer_right = mid + 1;
int k;
for (k = i; k <= j; k++)
{
if (pointer_left == mid + 1)
{
aux[k] = a[pointer_right];
pointer_right++;
}
else if (pointer_right == j + 1)
{
aux[k] = a[pointer_left];
pointer_left++;
}
else if (a[pointer_left] < a[pointer_right])
{
aux[k] = a[pointer_left];
pointer_left++;
}
else
{
aux[k] = a[pointer_right];
pointer_right++;
}
}
for (k = i; k <= j; k++)
{
a[k] = aux[k];
}
}
int main()
{
int a[10], aux[10], n, i, d, swap;
printf("Enter number of elements in the array:\n");
scanf("%d", &n);
printf("\nEnter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
merge_sort(0, n - 1, a, aux);
printf("\nArray after Merge Sort:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
return 0;
}