Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. template <class ForwardIt>
  2. void qSort(ForwardIt first, ForwardIt last) {
  3. ThreadPool threadPool(thread::hardware_concurrency());
  4. qSortAuxiliary(first, last, threadPool);
  5. }
  6.  
  7. template <class ForwardIt>
  8. void qSortAuxiliary(ForwardIt first, ForwardIt last, ThreadPool & threadPool) {
  9. typedef iterator_traits<ForwardIt>::value_type T;
  10. while (true) {
  11. if(first == last) return;
  12. auto pivot = first;
  13. advance(pivot, distance(first, last) / 2);
  14. auto middle1 = std::partition(first, last, [pivot](const T& em){ return em < *pivot; });
  15. auto middle2 = std::partition(middle1, last, [pivot](const T& em){ return !(*pivot < em); });
  16. threadPool.appendFn(bind(qSortAuxiliary<ForwardIt>, middle2, last, std::ref(threadPool)));
  17. last = middle1;
  18. }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement