fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iterator>
  5. #include <numeric>
  6.  
  7. int main() {
  8. const std::size_t size = 10;
  9. std::vector< int > v = {7, 8, 2, 9, 1, 3, 6, 5, 0, 4};
  10.  
  11. std::cout << "До сортировки: " << std::endl;
  12. std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
  13. //--- sort start
  14. std::size_t cntP = 0;
  15. std::size_t cntS = 0;
  16. std::size_t cntZ = 0;
  17. bool swapped = true;
  18. for (std::size_t i = 0; i < size && swapped; ++i)
  19. {
  20. swapped = false;
  21. ++cntP;
  22. for (std::size_t j = (i % 2) ? 0 : 1; j < size - 1; j += 2)
  23. {
  24. ++cntS;
  25. if (v[j] > v[j + 1])
  26. {
  27. swapped = true;
  28. ++cntZ;
  29. std::swap(v[j], v[j + 1]);
  30. }
  31. }
  32. }
  33. //--- sort end
  34. std::cout << std::endl;
  35. std::cout << "После сортировки: " << std::endl;
  36. std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
  37.  
  38. std::cout << std::endl;
  39. std::cout << "Количество проходов: " << cntP << std::endl;
  40. std::cout << "Количество сравнений: " << cntS << std::endl;
  41. std::cout << "Количество замен: " << cntZ << std::endl;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
До сортировки: 
7, 8, 2, 9, 1, 3, 6, 5, 0, 4, 
После сортировки: 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
Количество проходов: 9
Количество сравнений: 40
Количество замен: 29