Advertisement
Korotkodul

QuickSort_v1

Sep 30th, 2023 (edited)
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 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 get_med(vector<int> vv) {
  44.   for (int ii = 0; ii < 5; ++ii) {
  45.     for (int jj = 0; jj < 4; ++jj) {
  46.       if (vv[jj] > vv[jj + 1]) {
  47.         int* p1 = &vv[jj];
  48.         int* p2 = &vv[jj + 1];
  49.         swap(p1, p2);
  50.       }
  51.     }
  52.   }
  53.   return vv[2];
  54. }
  55.  
  56. int inf = 2e9;
  57.  
  58. vector<int> DQS(vector<int> aa) {};
  59.  
  60. int QuickSelect(vector<int> med) {
  61.   while (med.size() % 5 != 0) {
  62.     med.push_back(inf);
  63.   }
  64.   vector<int> new_med;
  65.   for (int ii = 0; ii < med.size(); ii += 5) {
  66.     vector<int> vv = {med[ii], med[ii + 1], med[ii + 2], med[ii + 3], med[ii + 4]};
  67.     new_med.push_back(vv[2]);
  68.   }
  69.   if (new_med.size() == 1) {
  70.     return new_med[0];
  71.   }
  72.   new_med = DQS(new_med);
  73.   int res = QuickSelect(new_med);
  74.   return res;
  75. }
  76.  
  77. vector<int> DQS()
  78.  
  79.  
  80.  
  81.  
  82.  
  83. int main() {
  84.   std::ios::sync_with_stdio(false);
  85.   std::cin.tie(0);
  86.   std::cout.tie(0);
  87.   int nn;
  88.   cin >> nn;
  89.   vector <int> aa(nn);
  90.   for (int& ii: aa) {
  91.     cin >> ii;
  92.   }
  93.   int xx;
  94.   cin >> xx;
  95.   aa = Partition(aa, xx);
  96.   for (int ii: aa) {
  97.     cout << ii << ' ';
  98.   }
  99.   cout << "\n";
  100. }
  101. /*
  102. 17
  103. 2 2 0 2 1 2 0 2 1 0 2 1 2 2 0 0 2
  104. 1
  105. */
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement