fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void sortArray(vector<int>&arr, int n)
  5. {
  6. int zeroCount = 0;
  7. int oneCount = 0;
  8. int twoCount = 0;
  9. for(int i = 0; i < n; i++)
  10. {
  11. if(arr[i] == 0)
  12. zeroCount++;
  13. else if(arr[i]==1)
  14. oneCount++;
  15. else
  16. twoCount++;
  17. }
  18. int index = 0;
  19. for(int i=0; i<zeroCount; i++)
  20. {
  21. arr[index++] = 0;
  22. }
  23. for(int i=0; i<oneCount; i++)
  24. {
  25. arr[index++] = 1;
  26. }
  27. for(int i=0; i<twoCount; i++)
  28. {
  29. arr[index++] = 2;
  30. }
  31. }
  32. int main() {
  33. // your code goes here
  34. int n;
  35. cin>>n;
  36. vector<int> arr(n);
  37. for(int i=0; i<n; i++)
  38. {
  39. cin>>arr[i];
  40. }
  41.  
  42. sortArray(arr, n);
  43. for(int i=0; i<n; i++)
  44. {
  45. cout<<arr[i]<<" ";
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5396KB
stdin
7
1 1 1 0 0 2 0
stdout
0 0 0 1 1 1 2