fork(1) download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. template <typename iter>
  6. void sort_b(iter first, iter last)
  7. {
  8. while(first != last)
  9. {
  10. iter current = first;
  11. while(2 <= last - current)
  12. {
  13. if(*current > *(current + 1)) std::iter_swap(current, current + 1);
  14. ++current;
  15. }
  16. --last;
  17. }
  18. }
  19.  
  20. int main()
  21. {
  22. std::vector<int> v { 1, 2, 3, 9, 8, 7, 6, 5, 4 };
  23.  
  24. std::cout << "input: ";
  25. std::for_each(v.begin(), v.end(), [](int i){
  26. std::cout << i << " ";
  27. });
  28. std::cout << std::endl;
  29.  
  30. sort_b(v.begin(), v.end());
  31.  
  32. std::cout << "output: ";
  33. std::for_each(v.begin(), v.end(), [](int i){
  34. std::cout << i << " ";
  35. });
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
input: 1 2 3 9 8 7 6 5 4 
output: 1 2 3 4 5 6 7 8 9