Advertisement
illfate

Untitled

Dec 24th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4.  
  5. using std::vector;
  6.  
  7. template<typename RandomIt>
  8. void SelectionSort(RandomIt begin, RandomIt end) {
  9.     for (auto it = begin + 1; it < end; ++it) {
  10.         auto current_value = *it;
  11.         auto prev_it = std::prev(it);
  12.         while (prev_it >= begin && *prev_it > current_value) {
  13.             *std::next(prev_it) = *prev_it;
  14.             --prev_it;
  15.         }
  16.         *std::next(prev_it) = current_value;
  17.     }
  18. }
  19.  
  20. struct person {
  21.     std::string name;
  22.     int age;
  23. };
  24.  
  25. bool operator<(const person &lhs, const person &rhs) {
  26.     return lhs.name < rhs.name;
  27. }
  28.  
  29. bool operator>(const person &lhs, const person &rhs) {
  30.     return lhs.name > rhs.name;
  31. }
  32.  
  33. bool operator==(const person &lhs, const person &rhs) {
  34.     return lhs.name == rhs.name;
  35. }
  36.  
  37. void RunSortTestExample() {
  38.     vector<int> vector1 = {1, 5, 7, 82, 3, 5, 7, 12};
  39.     SelectionSort(vector1.begin(), vector1.end());
  40.     for (auto x:vector1) {
  41.         std::cout << x << " ";
  42.     }
  43.     std::cout << std::endl;
  44.     int array[] = {1, 5, 72, 3, 42, 45, 2, 35, 6};
  45.     SelectionSort(std::begin(array), std::end(array));
  46.     for (auto x:array) {
  47.         std::cout << x << " ";
  48.     }
  49.     std::cout << std::endl;
  50.     vector<person> persons = {
  51.             {
  52.                     "ilya",
  53.                     18,
  54.             },
  55.             {
  56.                     "egor",
  57.                     19,
  58.             },
  59.             {
  60.                     "yana",
  61.                     22,
  62.             },
  63.             {
  64.                     "andrei",
  65.                     20,
  66.             }
  67.  
  68.     };
  69.     SelectionSort(persons.begin(), persons.end());
  70.     for (const auto&[name, age]:persons) {
  71.         std::cout << name << " " << age << std::endl;
  72.     }
  73. }
  74.  
  75. int main() {
  76.     RunSortTestExample();
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement