Advertisement
wowonline

Untitled

Apr 6th, 2022
798
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. template<typename IT, typename FUNC>
  5. void selection_sort(IT b, IT e, FUNC comp)
  6. {
  7.         for (auto it = b; it != e; ++it)
  8.         {
  9.             auto min_idx = it;
  10.             auto it2 = it++;
  11.             for (; it2 != e; ++it2) {
  12.                 if (comp(*it2, *min_idx)) {
  13.                     min_idx = it2;
  14.                 }
  15.             }
  16.  
  17.             auto t =  *min_idx;
  18.             *min_idx = *b;
  19.             *b = t;
  20.         }  
  21. }
  22.  
  23. template<typename IT>
  24. void selection_sort(IT b, IT e)
  25. {
  26.         for (auto it = b; it != e; ++it)
  27.         {
  28.             auto min_idx = it;
  29.             auto it2 = it++;
  30.             for (; it2 != e; ++it2) {
  31.                 if ((*it2) < (*min_idx)) {
  32.                     min_idx = it2;
  33.                 }
  34.             }
  35.  
  36.             auto t =  *min_idx;
  37.             *min_idx = *b;
  38.             *b = t;
  39.         }
  40. }
  41.  
  42. int main()
  43. {
  44.     std::vector<int> v = {1, 3, 2};
  45.     selection_sort(v.begin(), v.end());
  46.     // for (int c : v) {
  47.     //     std::cout<<c<<std::endl;
  48.     // }
  49. }
  50.  
  51. //https://www.geeksforgeeks.org/selection-sort/
Advertisement
RAW Paste Data Copied
Advertisement