diff --git a/lectures/12_advanced_recursion/README.md b/lectures/12_advanced_recursion/README.md index 2791a94..cb0d1a4 100644 --- a/lectures/12_advanced_recursion/README.md +++ b/lectures/12_advanced_recursion/README.md @@ -71,8 +71,10 @@ are you going to do with the result of the recursive call?) ## 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 -sorted vector. +- Idea: + - 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 values in subscript ranges [low..mid] (the lower interval) and [mid+1..high] (the upper interval) are each 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 - It exploits the power of recursion! We only need to think about + – Base case (intervals of size 1) – Splitting the vector – Merging the results @@ -96,7 +99,7 @@ Count the number of pairwise comparisons that are required. ## 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 148: Sort List](https://leetcode.com/problems/sort-list/). Solution: [p148_sortlist.cpp](../../leetcode/p148_sortlist.cpp) ## 12.7 Example: Word Search diff --git a/leetcode/p912_sortarray.cpp b/leetcode/p912_sortarray.cpp new file mode 100644 index 0000000..3c03160 --- /dev/null +++ b/leetcode/p912_sortarray.cpp @@ -0,0 +1,63 @@ +class Solution { +public: + vector mergeArrays(vector& v1, vector& v2){ + int size1 = v1.size(); + int size2 = v2.size(); + vector 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){ + while(index2 sortArray(vector& 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 nums1(mid, 0); + vector nums2(size-mid, 0); + for(int i=0;i