adding merge sort array problem
This commit is contained in:
@@ -71,8 +71,10 @@ are you going to do with the result of the recursive call?)
|
|||||||
|
|
||||||
## 12.4 Another Recursion Example: Merge Sort
|
## 12.4 Another Recursion Example: Merge Sort
|
||||||
|
|
||||||
- Idea: 1) Split a vector in half, 2) Recursively sort each half, and 3) Merge the two sorted halves into a single
|
- Idea:
|
||||||
sorted vector.
|
- 1) Split a vector in half,
|
||||||
|
- 2) Recursively sort each half, and
|
||||||
|
- 3) Merge the two sorted halves into a single sorted vector.
|
||||||
- Suppose we have a vector called values having two halves that are each already sorted. In particular, the
|
- Suppose we have a vector called values having two halves that are each already sorted. In particular, the
|
||||||
values in subscript ranges [low..mid] (the lower interval) and [mid+1..high] (the upper interval) are each
|
values in subscript ranges [low..mid] (the lower interval) and [mid+1..high] (the upper interval) are each
|
||||||
in increasing order.
|
in increasing order.
|
||||||
@@ -87,6 +89,7 @@ into the scratch vector. Finally, the entire scratch vector is copied back.
|
|||||||
## 12.5 Thinking About Merge Sort
|
## 12.5 Thinking About Merge Sort
|
||||||
|
|
||||||
- It exploits the power of recursion! We only need to think about
|
- It exploits the power of recursion! We only need to think about
|
||||||
|
|
||||||
– Base case (intervals of size 1)
|
– Base case (intervals of size 1)
|
||||||
– Splitting the vector
|
– Splitting the vector
|
||||||
– Merging the results
|
– Merging the results
|
||||||
@@ -96,7 +99,7 @@ Count the number of pairwise comparisons that are required.
|
|||||||
|
|
||||||
## 12.6 Merge Sort Exercises
|
## 12.6 Merge Sort Exercises
|
||||||
|
|
||||||
<!--- [Leetcode problem 912: Sort an Array](https://leetcode.com/problems/sort-an-array/). Solution: [p912_sortarray.cpp](../../leetcode/p912_sortarray.cpp)-->
|
- [Leetcode problem 912: Sort an Array](https://leetcode.com/problems/sort-an-array/). Solution: [p912_sortarray.cpp](../../leetcode/p912_sortarray.cpp)
|
||||||
- [Leetcode problem 148: Sort List](https://leetcode.com/problems/sort-list/). Solution: [p148_sortlist.cpp](../../leetcode/p148_sortlist.cpp)
|
- [Leetcode problem 148: Sort List](https://leetcode.com/problems/sort-list/). Solution: [p148_sortlist.cpp](../../leetcode/p148_sortlist.cpp)
|
||||||
|
|
||||||
## 12.7 Example: Word Search
|
## 12.7 Example: Word Search
|
||||||
|
|||||||
63
leetcode/p912_sortarray.cpp
Normal file
63
leetcode/p912_sortarray.cpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> mergeArrays(vector<int>& v1, vector<int>& v2){
|
||||||
|
int size1 = v1.size();
|
||||||
|
int size2 = v2.size();
|
||||||
|
vector<int> v(size1+size2, 0);
|
||||||
|
int index1 = 0;
|
||||||
|
int index2 = 0;
|
||||||
|
int index = 0;
|
||||||
|
// scan and compare, whoever is smaller goes to v.
|
||||||
|
while(index1<size1 && index2<size2){
|
||||||
|
if(v1[index1]<v2[index2]){
|
||||||
|
v[index] = v1[index1];
|
||||||
|
index1 = index1 + 1;
|
||||||
|
}else{
|
||||||
|
v[index] = v2[index2];
|
||||||
|
index2 = index2 + 1;
|
||||||
|
}
|
||||||
|
index = index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if v1 is done, let's now deal with v2 left overs
|
||||||
|
if(index1>=size1){
|
||||||
|
while(index2<size2){
|
||||||
|
v[index] = v2[index2];
|
||||||
|
index++;
|
||||||
|
index2++;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
// else means v2 is done. let's now deal with v1 left overs
|
||||||
|
while(index1<size1){
|
||||||
|
v[index] = v1[index1];
|
||||||
|
index++;
|
||||||
|
index1++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
vector<int> sortArray(vector<int>& nums) {
|
||||||
|
int size = nums.size();
|
||||||
|
// base case
|
||||||
|
if(size==1){
|
||||||
|
return nums;
|
||||||
|
}
|
||||||
|
// general case
|
||||||
|
// split evenly or not evenly does not matter that much in this case
|
||||||
|
int mid = size/2;
|
||||||
|
vector<int> nums1(mid, 0);
|
||||||
|
vector<int> nums2(size-mid, 0);
|
||||||
|
for(int i=0;i<mid;i++){
|
||||||
|
nums1[i]=nums[i];
|
||||||
|
}
|
||||||
|
for(int i=mid;i<size;i++){
|
||||||
|
// caution: here the index in nums2 has to be i-mid, rather than i; and if we use i here, it will cause a heap buffer overflow issue.
|
||||||
|
nums2[i-mid]=nums[i];
|
||||||
|
}
|
||||||
|
// sort the left half
|
||||||
|
nums1 = sortArray(nums1);
|
||||||
|
// sort the right half
|
||||||
|
nums2 = sortArray(nums2);
|
||||||
|
return mergeArrays(nums1, nums2);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user