-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
49 lines (45 loc) · 1.07 KB
/
Copy pathMergeSort.cpp
File metadata and controls
49 lines (45 loc) · 1.07 KB
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
#include <iostream>
#include <vector>
void mergeSort(std::vector<int>& nums, std::vector<int>& temp, int l, int r) {
if (l >= r) {
return;
}
int m = l + (r - l) / 2;
mergeSort(nums, temp, l , m);
mergeSort(nums, temp, m + 1, r);
// 优化, 避免拷贝
if (nums[m] <= nums[m + 1]) {
return;
}
int l_index = l;
int r_index = m + 1;
int i = l;
while(l_index <= m && r_index <= r) {
temp[i++] = nums[l_index] <= nums[r_index] ? nums[l_index++] : nums[r_index++];
}
while(l_index <= m) {
temp[i++] = nums[l_index++];
}
while(r_index <= r) {
temp[i++] = nums[r_index++];
}
// need copy
for (i = l; i <=r; ++i) {
nums[i] = temp[i];
}
}
void sort(std::vector<int>& nums) {
int n = nums.size();
if (n <= 1) {
return;
}
std::vector<int> temp(n);
mergeSort(nums, temp, 0, n - 1);
}
int main() {
std::vector<int> temp {-1, 3, 1, 2, 5, 3};
sort(temp);
for (auto each : temp) {
std::cout << "each: " << each << std::endl;
}
}