Created
November 10, 2017 07:01
-
-
Save hackallcode/af3917deeb8cb5d8e4eea4b57c875da3 to your computer and use it in GitHub Desktop.
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 <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::vector<typename IT::value_type> buf(first, halfIt); | |
std::merge(buf.begin(), buf.end(), halfIt, last, first); | |
} | |
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