-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort an Array.cpp
47 lines (44 loc) · 1.26 KB
/
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
/*
Problem Title: Sort an Array
Problem URL: https://leetcode.com/problems/sort-an-array/
Description: Given an array of integers nums, sort the array in ascending order.
Difficulty: Medium
Language: C++
Category: Algorithms
*/
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
mergeSort(nums, 0, nums.size()-1);
return nums;
}
private:
void mergeSort(vector<int>& nums, int start, int end)
{
if (start<end){
int mid = (start+end)/2;
mergeSort(nums, start, mid);
mergeSort(nums, mid+1, end);
merge(nums, start, mid, end);
}
}
private:
void merge(vector<int>& nums, int start, int mid, int end)
{
vector<int> firstHalf;
for (int i = start; i <= mid; ++i)
firstHalf.push_back(nums[i]);
int left=start, right=mid+1, k=start;
while(left<=mid&&right<=end)
{
if (firstHalf[left-start] < nums[right])
{nums[k++] = firstHalf[left-start]; left++;}
else
nums[k++] = nums[right++];
}
while(left<=mid)
{nums[k++] = firstHalf[left-start]; left++;}
while(right<=end)
nums[k++] = nums[right++];
}
};