fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. void customSort(vector<int>& arr) {
  7. vector<int> positive, zeros, negative;
  8.  
  9.  
  10. for (int num : arr) {
  11. if (num > 0)
  12. positive.push_back(num);
  13. else if (num == 0)
  14. zeros.push_back(num);
  15. else
  16. negative.push_back(num);
  17. }
  18.  
  19.  
  20. sort(positive.begin(), positive.end(), greater<int>());
  21. sort(negative.begin(), negative.end());
  22.  
  23. arr.clear();
  24. arr.insert(arr.end(), positive.begin(), positive.end());
  25. arr.insert(arr.end(), zeros.begin(), zeros.end());
  26. arr.insert(arr.end(), negative.begin(), negative.end());
  27. }
  28.  
  29. void printArray(const vector<int>& arr) {
  30. for (int num : arr)
  31. cout << num << " ";
  32. cout << endl;
  33. }
  34.  
  35. int main() {
  36. vector<int> arr = {3, -1, 0, 5, -2, 7, 0, 4, -3};
  37. customSort(arr);
  38. cout << "Mang da sap xep: ";
  39. printArray(arr);
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Mang da sap xep: 7 5 4 3 0 0 -3 -2 -1