-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0912.SortAnArray.cpp
More file actions
53 lines (46 loc) · 1.41 KB
/
0912.SortAnArray.cpp
File metadata and controls
53 lines (46 loc) · 1.41 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
50
51
52
53
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Calculation variables.
const int n = nums.size();
vector<int> buffer(nums.size());
vector<int>* workingBuffers[2] = { &nums, &buffer };
// Perform merge sort (i think...)
int currentDiv = 1;
while (currentDiv < n) {
// Update divider.
const int m = currentDiv;
currentDiv *= 2;
// Sort.
for (int i = 0; i < n; i += currentDiv) {
// Get mid + end point.
const int mid = min(i + m, n), end = min(i + currentDiv, n);
// Work through two-pointers.
int index = i, left = i, right = mid;
while (left < mid && right < end) {
if ((*workingBuffers[0])[left] < (*workingBuffers[0])[right]) {
// Left is smaller.
(*workingBuffers[1])[index++] = (*workingBuffers[0])[left++];
} else {
// Right is smaller.
(*workingBuffers[1])[index++] = (*workingBuffers[0])[right++];
}
}
// Catch any remaining values.
while (left < mid)
(*workingBuffers[1])[index++] = (*workingBuffers[0])[left++];
while (right < end)
(*workingBuffers[1])[index++] = (*workingBuffers[0])[right++];
}
// Swap working buffer.
vector<int>* const tempBuffer = workingBuffers[0];
workingBuffers[0] = workingBuffers[1];
workingBuffers[1] = tempBuffer;
}
return *workingBuffers[0];
}
};