Skip to content

Instantly share code, notes, and snippets.

@htmlboss
Last active October 7, 2016 13:20
Show Gist options
  • Save htmlboss/3aa1714c65c9c9568bc3b3b33016d5b5 to your computer and use it in GitHub Desktop.
Save htmlboss/3aa1714c65c9c9568bc3b3b33016d5b5 to your computer and use it in GitHub Desktop.
Stable Merge Sort using C++14
#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