Last active
October 7, 2016 13:20
-
-
Save htmlboss/3aa1714c65c9c9568bc3b3b33016d5b5 to your computer and use it in GitHub Desktop.
Stable Merge Sort using C++14
This file contains 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
#include <algorithm> | |
#include <iterator> | |
#include <functional> | |
template<typename BiDirIt, typename Compare = std::less<> > | |
void Merge_Sort(BiDirIt first, BiDirIt last, Compare comp = Compare {}) { | |
const auto n = std::distance(first, last); | |
if (n > 1) { | |
const auto middle = std::next(first, n / 2); | |
Merge_Sort(first, middle, cmp); | |
Merge_Sort(middle, last, cmp); | |
std::inplace_merge(first, middle, last, cmp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment