fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function to sort binary array in linear time
  5. int Partition(int A[], int n)
  6. {
  7.  
  8. int j = 0;
  9.  
  10. // each time we encounter a 0, j is incremented and
  11. // 0 is placed before the pivot
  12. for (int i = 0; i < n; i++)
  13. {
  14. if (A[i] %2==0)
  15. {
  16. swap(A[i], A[j]);
  17. j++;
  18. }
  19. }
  20. }
  21.  
  22. // main function
  23. int main()
  24. {
  25. int A[] = { 2, 10, 3, 7, 8, 9, 10, 17 };
  26. int n = sizeof(A)/sizeof(A[0]);
  27.  
  28. Partition(A, n);
  29.  
  30. // print the rearranged array
  31. for (int i = 0 ; i < n; i++)
  32. cout << A[i] << " ";
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
2 10 8 10 3 9 7 17