fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5.  
  6. std::vector<int> &selection_sort (std::vector<int> &v)
  7. {
  8. for (auto it = v.begin(); it != v.end() - 1; ++it)
  9. {
  10. auto smallest = std::min_element (it + 1, v.end());
  11. std::cout << "it = " << *it << std::endl;
  12. std::cout << "(it + 1) = " << *(it + 1) << std::endl;
  13. std::cout << "smallest = " << *smallest << std::endl;
  14. std::iter_swap (it, smallest);
  15. }
  16. return v;
  17. }
  18.  
  19. int main()
  20. {
  21. std::vector<int> v = {8, 2, 3, 4, 7, 1, 5, 9, 6, 10};
  22. v = selection_sort (v);
  23. for (auto &i : v)
  24. std::cout << i << " ";
  25. std::cout << std::endl;
  26. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
it = 8
(it + 1) = 2
smallest = 1
it = 2
(it + 1) = 3
smallest = 3
it = 2
(it + 1) = 4
smallest = 4
it = 2
(it + 1) = 7
smallest = 5
it = 7
(it + 1) = 8
smallest = 2
it = 8
(it + 1) = 7
smallest = 6
it = 7
(it + 1) = 9
smallest = 8
it = 9
(it + 1) = 7
smallest = 7
it = 9
(it + 1) = 10
smallest = 10
1 3 4 5 2 6 8 7 10 9