Advertisement
illfate

Untitled

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