-
Notifications
You must be signed in to change notification settings - Fork 1
/
Merge_sort_recursive.c
58 lines (58 loc) · 1.06 KB
/
Merge_sort_recursive.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
#include <stdio.h>
#include <stdlib.h>
void merge(int A[], int l, int mid, int h)
{
int i = l, j = (mid + 1);
int *c;
int k = l; // remember
c = (int *)malloc((h + 1) * sizeof(int));
while (i <= mid && j <= h)
{
if (A[i] < A[j])
{
c[k] = A[i];
i++;
k++;
}
else if (A[j] < A[i])
{
c[k] = A[j];
k++;
j++;
}
}
for (; i <= mid; i++)
{
c[k] = A[i];
k++;
}
for (; j <= h; j++)
{
c[k] = A[j];
k++;
}
for (int i = l; i <= h; i++)
{
A[i] = c[i];
}
}
void merge_sort(int A[], int l, int h)
{
int mid;
if (l < h)
{
mid = (l + h) / 2;
merge_sort(A, l, mid);
merge_sort(A, mid + 1, h);
merge(A, l, mid, h);
}
}
int main()
{
int A[] = {2, 3, 5, 4, 78, 56, 43, 21};
merge_sort(A, 0, 7);
for (int i = 0; i < 8; i++)
{
printf("%d ", A[i]);
}
}