Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template<class Iterator>
  8. static constexpr void radix_sort(Iterator begin, Iterator end) noexcept {
  9. constexpr auto partition_recursive = [](auto self, auto index, auto begin, auto end) __attribute__((always_inline)) {
  10. using index_t = decltype(index);
  11. const auto mid = partition(begin, end, [](auto x) {
  12. return (x >> index_t::value & 1) == 0;
  13. });
  14. if constexpr (index_t::value > 0) {
  15. for (const auto& [from, to] : { pair { begin, mid }, pair { mid, end } })
  16. if (distance(from, to) > 1)
  17. self(self, integral_constant<size_t, index_t::value - 1>{}, from, to);
  18. }
  19. };
  20. partition_recursive(partition_recursive, integral_constant<size_t, (sizeof(typename Iterator::value_type) << 3) - 1>{}, begin, end);
  21. }
  22.  
  23. int main() {
  24. vector<uint16_t> v(10 * 1000 * 1000);
  25. for (auto& n : v)
  26. n = rand() % numeric_limits<uint16_t>::max();
  27. cout << boolalpha << is_sorted(v.begin(), v.end()) << endl;
  28. radix_sort(v.begin(), v.end());
  29. cout << is_sorted(v.begin(), v.end()) << endl;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement