Skip to content

Instantly share code, notes, and snippets.

@henriquerichter
Created July 29, 2017 15:23
Show Gist options
  • Select an option

  • Save henriquerichter/abd6d27379e271505e28633f5b47acf0 to your computer and use it in GitHub Desktop.

Select an option

Save henriquerichter/abd6d27379e271505e28633f5b47acf0 to your computer and use it in GitHub Desktop.
Implementation of MergeSort using STL
//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