-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_an_array.cpp
More file actions
49 lines (47 loc) · 1.35 KB
/
Copy pathsort_an_array.cpp
File metadata and controls
49 lines (47 loc) · 1.35 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 <vector>
using namespace std;
class Solution {
public:
void merge(vector<int>& nums, int left, int mid, int right) {
vector<int> temp;
int i = left;
int j = right;
while (i <= mid && j <= right) {
if (nums[i] <= nums[j]) {
temp.push_back(nums[i]);
i++;
}else {
temp.push_back(nums[j]);
j++;
}
}
//copy the remaining elements from the left half
while (i <= mid) {
temp.push_back(nums[i]);
i++;
}
//copy the remaining elements from the right half
while (j <= right) {
temp.push_back(nums[j]);
j++;
}
//copy the sorted elements back to the original array
for (int k=0; k<temp.size(); k++) {
nums[left+k] = temp[k];
}
}
void mergeSort(vector<int>& nums, int left, int right) {
if (left >= right) return;
//find the middle index
int mid = left + (right - left) / 2;
//sort the left half
mergeSort(nums, left, mid);
mergeSort(nums, mid+1, right);
//merge the two halves
merge(nums, left, mid, right);
}
vector<int> sortArray(vector<int>& nums) {
mergeSort(nums, 0, nums.size()-1);
return nums;
}
};