fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. void partition(vector<int>& A) {
  7. int n = A.size(), start = 1, end = n-1, p = A[0];
  8. while(start < end) {
  9. while(A[start] <= p)
  10. start++;
  11. while(A[end] > p)
  12. end--;
  13. if(start < end) {
  14. int temp = A[start];
  15. A[start] = A[end];
  16. A[end] = temp;
  17. }
  18. }
  19. if(A[start] > p)
  20. start = start - 1;
  21. int temp = A[0];
  22. A[0] = A[start];
  23. A[start] = temp;
  24. }
  25.  
  26.  
  27. int main() {
  28. // your code goes here
  29. vector<int> num = {6, 11, 2, 1, 8, 3};
  30. partition(num);
  31. for(int data : num)
  32. cout << data << " " ;
  33. return 0;
  34. }
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
1 3 2 6 8 11