Created
July 29, 2017 15:23
-
-
Save henriquerichter/abd6d27379e271505e28633f5b47acf0 to your computer and use it in GitHub Desktop.
Implementation of MergeSort using STL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //http://ysonggit.github.io/2015/01/26/efficient-implementation-of-mergesort-using-stl.html | |
| #include <vector> | |
| #include <iostream> | |
| #include <algorithm> | |
| template<class Iter> | |
| void merge_sort(Iter first, Iter last){ | |
| if (last - first > 1) { | |
| Iter middle = first + (last - first) / 2; | |
| merge_sort(first, middle); // [first, middle) | |
| merge_sort(middle, last); // [middle, last) | |
| std::inplace_merge(first, middle, last); | |
| } | |
| } | |
| int main(){ | |
| std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3}; | |
| merge_sort(v.begin(), v.end()); | |
| for(auto n : v) { | |
| std::cout << n << ' '; | |
| } | |
| std::cout << '\n'; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment