Advertisement
Korotkodul

QuickSort

Sep 30th, 2023 (edited)
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::max;
  8. using std::min;
  9. using std::string;
  10. using std::vector;
  11.  
  12. void swap(int* p1, int* p2) {
  13.   int t = *p1;
  14.   *p1 = *p2;
  15.   *p2 = t;
  16. }
  17.  
  18.  
  19. vector<int> Partition(vector<int> aa, int xx) {
  20.   //делим на < и >=
  21.   int nn = aa.size();
  22.   int ll = -1;
  23.   for (int ii = 0; ii < nn; ++ii) {
  24.     if (aa[ii] < xx) {
  25.       int* p1 = &aa[ii];
  26.       int* p2 = &aa[ll + 1];
  27.       swap(p1, p2);
  28.       ll++;
  29.     }
  30.   }
  31.   //делим на = и >
  32.   for (int ii = ll + 1; ii < nn; ++ii) {
  33.     if (aa[ii] == xx) {
  34.       int* p1 = &aa[ii];
  35.       int* p2 = &aa[ll + 1];
  36.       swap(p1, p2);
  37.       ll++;
  38.     }
  39.   }
  40.   return aa;
  41. }
  42.  
  43. int main() {
  44.   std::ios::sync_with_stdio(false);
  45.   std::cin.tie(0);
  46.   std::cout.tie(0);
  47.   int nn;
  48.   cin >> nn;
  49.   vector <int> aa(nn);
  50.   for (int& ii: aa) {
  51.     cin >> ii;
  52.   }
  53.   int xx;
  54.   cin >> xx;
  55.   aa = Partition(aa, xx);
  56.   for (int ii: aa) {
  57.     cout << ii << ' ';
  58.   }
  59.   cout << "\n";
  60. }
  61. /*
  62. 17
  63. 2 2 0 2 1 2 0 2 1 0 2 1 2 2 0 0 2
  64. 1
  65. */
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement