-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMerge-sort.cpp
69 lines (58 loc) · 1.13 KB
/
Merge-sort.cpp
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
#include <iostream>
using namespace std;
void merge(int arr[], int l, int m, int r) {
int nl = m - l + 1;
int nr = r - m;
int lh[nl], rh[nr];
for (int i = 0; i < nl; ++i) {
lh[i] = arr[l + i];
}
for (int i = 0; i < nr; ++i) {
rh[i] = arr[m + i + 1];
}
int i = 0, j = 0, k = l;
while (i < nl && j < nr) {
if (lh[i] <= rh[j]) {
arr[k] = lh[i];
i++;
} else {
arr[k] = rh[j];
j++;
}
k++;
}
while (i < nl) {
arr[k] = lh[i];
i++;
k++;
}
while (j < nr) {
arr[k] = rh[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l >= r) {
return;
} else {
int mid = l + (r - l) / 2;
mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
}
void print_arr(int array[], int n) {
for (int i = 0; i < n; ++i) {
cout << array[i] << " ";
}
std::cout << std::endl;
}
int main(int argc, char const *argv[]) {
int array[] = {22, 4, 5, 4, 8, 7, 8, 7, 8, 9, 10, 1, 0};
int n = sizeof(array) / sizeof(array[0]);
print_arr(array, n);
mergeSort(array, 0, n - 1);
print_arr(array, n);
return 0;
}