-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.cpp
82 lines (46 loc) · 1.13 KB
/
mergesort.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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
void merge(vector<T>& array, int start, int mid, int end) {
int i = start;
int j = mid + 1;
int k = 0;
vector<T> result;
result.resize(end - start + 1);
while (i <= mid && j <= end) {
if (array[i] < array[j]) result[k++] = array[i++];
else result[k++] = array[j++];
}
while (j <= end) {
result[k++] = array[j++];
}
while (i <= mid) {
result[k++] = array[i++];
}
k = 0;
for (int i = start; i <= end; i++, k++) {
array[i] = result[k];
}
}
//분할
template<typename T>
void merge_sort(vector<T>& array, int start, int end) {
int mid; // 중간값
//if (start >= end) return;//start가 end보가 더 커지면 리턴.
if (start < end) {
mid = (end + start) / 2;//중간값.
merge_sort(array, start, mid);//왼쪽으로 계속 분할
merge_sort(array, mid + 1, end);//오른쪽 분할. 중위순회 방식.
merge(array, start, mid, end);
}
}
int main(void)
{
vector<double> array = { 3.4, 8.2, 0.4, 2.1, 1, 4 };
merge_sort(array, 0, array.size() - 1);
for (int i = 0; i < (int)array.size(); i++) {
printf("%lf ", array[i]);
}
return 0;
}