fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <algorithm>
  4. #include <functional>
  5.  
  6. int main()
  7. {
  8. std::array<int, 8> array = {{ 18 ,5 ,24 ,9 ,12 ,6 ,2, 3 }};
  9.  
  10. std::cout << "Before first partitioning: ";
  11. for (auto const value : array)
  12. std::cout << value << ' ';
  13. std::cout << '\n';
  14.  
  15. // First partition putting all odd number even divisable by three first
  16. auto second_start = std::partition(std::begin(array), std::end(array), [](int const& value) {
  17. return (value % 2 != 0 && value % 3 == 0);
  18. });
  19.  
  20. std::cout << "Before second partitioning: ";
  21. for (auto const value : array)
  22. std::cout << value << ' ';
  23. std::cout << '\n';
  24.  
  25. auto third_start = std::partition(second_start, std::end(array), [](int const& value) {
  26. return !(value % 2 == 0 && value % 3 == 0);
  27. });
  28.  
  29. std::cout << "Before sorting: ";
  30. for (auto const value : array)
  31. std::cout << value << ' ';
  32. std::cout << '\n';
  33.  
  34. std::sort(std::begin(array), second_start, std::less<int>());
  35. std::sort(second_start, third_start, std::less<int>());
  36. std::sort(third_start, std::end(array), std::greater<int>());
  37.  
  38. std::cout << "After sorting: ";
  39. for (auto const value : array)
  40. std::cout << value << ' ';
  41. std::cout << '\n';
  42. }
  43.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Before first partitioning: 18 5 24 9 12 6 2 3 
Before second partitioning: 3 9 24 5 12 6 2 18 
Before sorting: 3 9 2 5 12 6 24 18 
After sorting: 3 9 2 5 24 18 12 6