Advertisement
illfate

Untitled

Dec 25th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <iterator>
  5. #include <unordered_map>
  6. #include <map>
  7. #include <type_traits>
  8. #include <algorithm>
  9. using std::vector;
  10.  
  11. template<typename RandomIt>
  12. void InsertionSort(RandomIt begin, RandomIt end) {
  13.     for (auto it = begin + 1; it < end; ++it) {
  14.         auto current_value = *it;
  15.         auto prev_it = std::prev(it);
  16.         while (prev_it >= begin && *prev_it > current_value) {
  17.             *std::next(prev_it) = *prev_it;
  18.             --prev_it;
  19.         }
  20.         *std::next(prev_it) = current_value;
  21.     }
  22. }
  23.  
  24. template<typename RandomIt,class = typename std::enable_if<std::is_integral<typename std::iterator_traits<RandomIt>::value_type>::value>::type >
  25. void CountingSortInt(RandomIt begin, RandomIt end) {
  26.     using ValueType = typename std::iterator_traits<RandomIt>::value_type;
  27.     size_t max = *std::max_element(begin, end);
  28.     std::vector<ValueType> count;
  29.     count.resize(max+1);
  30.     for ( auto value=begin; value!=end ;++value){
  31.         ++count[*value];
  32.     }
  33.     size_t counter=-1;
  34.     for (auto it=count.begin(); it!=count.end() ;++it){
  35.         ++counter;
  36.         ValueType value = counter;
  37.         size_t size = *it;
  38.         if(size==0){
  39.             continue;
  40.         }
  41.         std::fill_n(begin, size, value);
  42.         std::advance(begin, size);
  43.     }
  44. }
  45.  
  46. template<typename RandomIt,class = typename std::enable_if<!std::is_integral<typename std::iterator_traits<RandomIt>::value_type>::value>::type >
  47. void CountingSortNonInt(RandomIt begin, RandomIt end) {
  48.     using ValueType = typename std::iterator_traits<RandomIt>::value_type;
  49.     std::map<ValueType, std::size_t> counts;
  50.  
  51.     for (auto value = begin; value != end; ++value) {
  52.         ++counts[*value];
  53.     }
  54.  
  55.     for (auto &count: counts) {
  56.         ValueType value = count.first;
  57.         size_t size = count.second;
  58.         std::fill_n(begin, size, value);
  59.         std::advance(begin, size);
  60.     }
  61. }
  62.  
  63. struct person {
  64.     std::string name;
  65.     int age;
  66. };
  67.  
  68. bool operator<(const person &lhs, const person &rhs) {
  69.     return lhs.age < rhs.age;
  70. }
  71.  
  72. bool operator>(const person &lhs, const person &rhs) {
  73.     return lhs.age > rhs.age;
  74. }
  75.  
  76. bool operator==(const person &lhs, const person &rhs) {
  77.     return lhs.age == rhs.age;
  78. }
  79.  
  80. void RunSortTestExample() {
  81.     vector<int> vector1 = {1, 5, 7, 82, 3, 5, 7, 12};
  82.     InsertionSort(vector1.begin(), vector1.end());
  83.     for (auto x:vector1) {
  84.         std::cout << x << " ";
  85.     }
  86.     std::cout << std::endl;
  87.     int array[] = {1, 5, 72, 3, 42, 45, 2, 35, 6};
  88.     InsertionSort(std::begin(array), std::end(array));
  89.     for (auto x:array) {
  90.         std::cout << x << " ";
  91.     }
  92.     std::cout << std::endl;
  93.     vector<person> persons = {
  94.             {
  95.                     "ilya",
  96.                     18,
  97.             },
  98.             {
  99.                     "egor",
  100.                     19,
  101.             },
  102.             {
  103.                     "yana",
  104.                     22,
  105.             },
  106.             {
  107.                     "andrei",
  108.                     20,
  109.             }
  110.  
  111.     };
  112.     InsertionSort(persons.begin(), persons.end());
  113.     for (const auto&[name, age]:persons) {
  114.         std::cout << name << " " << age << std::endl;
  115.     }
  116.     std::cout<<std::endl;
  117. }
  118.  
  119. void RunCountingSortTestExample() {
  120.     vector<int> vector1 = {1, 5, 7, 82, 3, 5, 7, 12};
  121.     CountingSortInt(vector1.begin(), vector1.end());
  122.     for (auto x:vector1) {
  123.         std::cout << x << " ";
  124.     }
  125.     std::cout << std::endl;
  126.      int array[] = {1, 5, 72, 3, 42, 45, 2, 35, 6};
  127.     CountingSortInt(std::begin(array), std::end(array));
  128.      for (auto x:array) {
  129.          std::cout << x << " ";
  130.      }
  131.      std::cout << std::endl;
  132.      vector<person> persons = {
  133.              {
  134.                      "ilya",
  135.                      18,
  136.              },
  137.              {
  138.                      "egor",
  139.                      19,
  140.              },
  141.              {
  142.                      "yana",
  143.                      22,
  144.              },
  145.              {
  146.                      "andrei",
  147.                      20,
  148.              }
  149.  
  150.      };
  151.      CountingSortNonInt(persons.begin(), persons.end());
  152.      for (const auto&[name, age]:persons) {
  153.          std::cout << name << " " << age << std::endl;
  154.      }
  155. }
  156.  
  157. int main() {
  158.      RunSortTestExample();
  159.     RunCountingSortTestExample();
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement