Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 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)) noexcept {
  10. using index_t = decltype(index);
  11. const auto mid = partition(begin, end, [](auto x) noexcept { return (x >> index_t::value & 1) == 0; });
  12. if constexpr (index_t::value > 0) {
  13. for (auto p : { false, true })
  14. if (distance(p ? mid : begin, p ? end : mid) > 1)
  15. self(self, integral_constant<size_t, index_t::value - 1>{}, p ? mid : begin, p ? end : mid);
  16. }
  17. };
  18. partition_recursive(partition_recursive, integral_constant<size_t, (sizeof(typename Iterator::value_type) << 3) - 1>{}, begin, end);
  19. }
  20.  
  21. int main() {
  22. vector<uint16_t> v(10 * 1000 * 1000);
  23. for (auto& n : v) n = rand() % numeric_limits<uint16_t>::max();
  24. cout << boolalpha << is_sorted(v.begin(), v.end()) << endl;
  25. radix_sort(v.begin(), v.end());
  26. cout << is_sorted(v.begin(), v.end()) << endl;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement