Advertisement
Georgiy031

Untitled

Apr 14th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <locale.h>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. void surface(vector<int>& v, int i, int k) {
  8.     int j, m, copy;
  9.     copy = v[i - 1];
  10.     m = 2 * i;
  11.     while (m <= k) {
  12.         if (m == k) j = m;
  13.         else if (v[m - 1] > v[m]) j = m;
  14.         else j = m + 1;
  15.  
  16.         if (v[j - 1] > copy) {
  17.             v[i - 1] = v[j - 1];
  18.             i = j;
  19.             m = 2 * i;
  20.         }
  21.         else break;
  22.     }
  23.     v[i - 1] = copy;
  24. }
  25. void floid_sort(vector<int>& v) {
  26.     for (int i = v.size() / 2; i >= 2; --i) surface(v, i, v.size());
  27.     for (int i = v.size(); i >= 2; --i) {
  28.         surface(v, 1, i);
  29.         swap(v[i - 1], v[0]);
  30.     }
  31.  
  32. }
  33.  
  34. int main() {
  35.     setlocale(LC_ALL, "Russian");
  36.     int n;
  37.     cout << "Введите длину массива: ";
  38.     cin >> n;
  39.     cout << "Введите массив: ";
  40.     vector<int> v(n);
  41.     for (auto& x : v) cin >> x;
  42.     floid_sort(v);
  43.     for (auto& x : v) cout << x << " ";
  44.     cout << endl;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement