Skip to content

Instantly share code, notes, and snippets.

@drewxa
Forked from hackallcode/LoHiSort.cpp
Last active November 12, 2017 11:49
Show Gist options
  • Save drewxa/674ccf8837ce67fd32faae1c1911b8cd to your computer and use it in GitHub Desktop.
Save drewxa/674ccf8837ce67fd32faae1c1911b8cd to your computer and use it in GitHub Desktop.
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <thread>
#include <future>
#include <iostream>
template<class IT>
void merge_sort_th(IT first, IT last, size_t min_len = 16)
{
auto len = std::distance(first, last);
if (len < min_len)
{
std::sort(first, last);
return;
}
IT halfIt = first + len/2;
std::thread loSort(merge_sort_th<IT>, first, halfIt, min_len);
merge_sort_th(halfIt, last, min_len);
loSort.join();
std::inplace_merge(first, halfIt, last);
}
int main()
{
std::vector<int> a(1001);
for (int i = 0; i < a.size(); ++i)
{
a[i] = rand() % 91;
}
merge_sort_th(a.begin(), a.end());
for (auto elem : a) {
std::cout << elem << ' ';
}
std::cout << std::endl << std::boolalpha << std::is_sorted(a.begin(), a.end());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment