Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <vector>
- using std::cin;
- using std::cout;
- using std::max;
- using std::min;
- using std::string;
- using std::vector;
- void swap(int* p1, int* p2) {
- int t = *p1;
- *p1 = *p2;
- *p2 = t;
- }
- vector<int> Partition(vector<int> aa, int xx) {
- //делим на < и >=
- int nn = aa.size();
- int ll = -1;
- for (int ii = 0; ii < nn; ++ii) {
- if (aa[ii] < xx) {
- int* p1 = &aa[ii];
- int* p2 = &aa[ll + 1];
- swap(p1, p2);
- ll++;
- }
- }
- //делим на = и >
- for (int ii = ll + 1; ii < nn; ++ii) {
- if (aa[ii] == xx) {
- int* p1 = &aa[ii];
- int* p2 = &aa[ll + 1];
- swap(p1, p2);
- ll++;
- }
- }
- return aa;
- }
- int main() {
- std::ios::sync_with_stdio(false);
- std::cin.tie(0);
- std::cout.tie(0);
- int nn;
- cin >> nn;
- vector <int> aa(nn);
- for (int& ii: aa) {
- cin >> ii;
- }
- int xx;
- cin >> xx;
- aa = Partition(aa, xx);
- for (int ii: aa) {
- cout << ii << ' ';
- }
- cout << "\n";
- }
- /*
- 17
- 2 2 0 2 1 2 0 2 1 0 2 1 2 2 0 0 2
- 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement