-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.c
77 lines (66 loc) · 1.34 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
//
// Created by ge on 2023/12/31.
//
#include <stdio.h>
void merge(int arr[], int L, int M, int R) {
int LEFT_SIZE = M - L;
int RIGHT_SIZE = R - M + 1;
int left[LEFT_SIZE];
int right[RIGHT_SIZE];
int i, j, k;
for (i = L; i < M; i++) {
left[i - L] = arr[i];
}
for (i = M; i <= R; i++) {
right[i - M] = arr[i];
}
i = 0;
j = 0;
k = L;
while (i < LEFT_SIZE && j < RIGHT_SIZE) {
if (left[i] < right[j]) {
arr[k] = left[i];
i++;
k++;
} else {
arr[k] = right[j];
j++;
k++;
}
}
while (i < LEFT_SIZE) {
arr[k] = left[i];
i++;
k++;
}
while (j < RIGHT_SIZE) {
arr[k] = right[j];
j++;
k++;
}
}
void mergeSort(int arr[], int L, int R) {
if (L == R) {
return;
} else {
int M = (L + R) / 2;
mergeSort(arr, L, M);
mergeSort(arr, M + 1, R);
merge(arr, L, M + 1, R);
}
}
void printArr(int arr[], int len) {
for (int i = 0; i < len; i++) {
printf("%d\t", arr[i]);
}
}
int main() {
int arr[] = {2, 8, 9, 10, 4, 5, 6, 7};
int L = 0;
int R = 7;
// int M = (L + R) / 2;
// merge(arr, L, M+1, R);
mergeSort(arr, L, R);
printArr(arr, 8);
// printArr(arr,8);
}