fork(6) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <list>
  4.  
  5. void biswap(std::list<int> &l)
  6. {
  7. if (l.size() < 2)
  8. return;
  9. auto it2 = l.begin();
  10. auto it1 = it2++;
  11. auto e = l.end();
  12. for (;;)
  13. {
  14. if (*it1 < *it2)
  15. std::swap(*it1, *it2);
  16. it1 = it2++;
  17. if (it2 == e)
  18. return;
  19. it1 = it2++;
  20. if (it2 == e)
  21. return;
  22. }
  23. }
  24.  
  25.  
  26. int main() {
  27. std::list<int> myList {3, 2, 1, 2, 1, 3, 2};
  28. biswap(myList);
  29. for (int i: myList)
  30. std::cout << i << ", ";
  31. return 0;
  32. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
3, 2, 2, 1, 3, 1, 2,