-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0912-sort-an-array.cpp
52 lines (41 loc) · 1.23 KB
/
0912-sort-an-array.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
/*
Given an array of integers nums, sort the array in ascending order and return it.
Ex. nums = [5,2,3,1] -> [1,2,3,5]
Use Merge sort to sort the array.
Time - O(nlogn)
Space - O(n)
*/
class Solution {
private:
void merge(vector<int> &nums, int low, int mid, int high) {
if(low >= high)
return;
int l = low, r = mid + 1, k = 0, size = high - low + 1;
vector<int> sorted(size, 0);
while (l <= mid and r <= high){
if(nums[l] < nums[r])
sorted[k++] = nums[l++];
else
sorted[k++] = nums[r++];
}
while(l <= mid)
sorted[k++] = nums[l++];
while(r <= high)
sorted[k++] = nums[r++];
for(k = 0; k < size; k++)
nums[k + low] = sorted[k];
}
void mergeSort(vector<int>& nums, int low, int high){
if(low >= high)
return;
int mid = low + (high - low) / 2;
mergeSort(nums, low, mid);
mergeSort(nums, mid + 1, high);
merge(nums, low, mid, high);
}
public:
vector<int> sortArray(vector<int>& nums) {
mergeSort(nums, 0, nums.size()-1);
return nums;
}
};