Advertisement
evcamels

timp4

Jun 6th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7.  
  8. bool isRav(vector <char> th) {
  9.     int a = 0;
  10.     for (int i = 0; i < th.size(); i++) {
  11.         for (int j = 0; j < th.size(); j++) {
  12.             if (th[i] == th[j]) {
  13.                 a++;
  14.             }
  15.         }
  16.     }
  17.     if (a == th.size() * th.size()) {
  18.         return true;
  19.     }
  20.     else {
  21.         return false;
  22.     }
  23. }
  24.  
  25. int sh3 = 0;
  26.  
  27. vector <char> Thanos(vector <char> th) {
  28.     if (th.size() == 1) {
  29.         return th;
  30.     }
  31.     else if (th.size() == 2) {
  32.         if (th[0] > th[1]) {
  33.             swap(th[0], th[1]);
  34.             sh3++;
  35.         }
  36.         return th;
  37.     }
  38.     else {
  39.         float a = 0;
  40.         for (int i = 0; i < th.size(); i++) {
  41.             float ia = th[i] - '0';
  42.             a = a + ia;
  43.         }
  44.         float sr = a / th.size();
  45.         char* asd = new char[th.size()];
  46.         int b = 0; //левая граница
  47.         int c = th.size() - 1; //праввая граница
  48.         for (int i = 0; i < th.size() && b != c + 1; i++) {
  49.             float ia = th[i] - '0';
  50.             if (ia <= sr) {
  51.                 asd[b] = th[i];
  52.                 sh3++;
  53.                 b++;
  54.             }
  55.             else if (ia > sr) {
  56.                 asd[c] = th[i];
  57.                 sh3++;
  58.                 c--;
  59.             }
  60.         }
  61.         int ch = th.size() / 2;
  62.         vector <char> th1;
  63.         vector <char> th2;
  64.         for (int i = 0; i < b; i++) {
  65.             th1.push_back(asd[i]);
  66.         }
  67.         for (int i = b; i < th.size(); i++) {
  68.             th2.push_back(asd[i]);
  69.         }
  70.         if (!isRav(th1)) {
  71.             th1 = Thanos(th1);
  72.         }
  73.         if (!isRav(th2)) {
  74.             th2 = Thanos(th2);
  75.         }
  76.         vector <char> th3;
  77.         for (int i = 0; i < th1.size(); i++) {
  78.             th3.push_back(th1[i]);
  79.         }
  80.         for (int i = 0; i < th2.size(); i++) {
  81.             th3.push_back(th2[i]);
  82.         }
  83.         return th3;
  84.     }
  85. }
  86.  
  87. int main() {
  88.     setlocale(0, "");
  89.     cout << "Введите строку: ";
  90.     string vr;
  91.     getline(cin, vr);
  92.     vector <char> strk;
  93.     for (int i = 0; i < vr.size(); i++) {
  94.         strk.push_back(vr[i]);
  95.     }
  96.     for (int i = 0; i < strk.size(); i++) {
  97.         cout << strk[i] << " ";
  98.     }
  99.     cout << endl;
  100.     vector <char> strk1;
  101.     strk1 = strk;
  102.     cout << "Время работы программы = " << clock() << endl;
  103.     cout << "Сортировка вставками" << endl;
  104.     int sh = 0; //счётчик перестановок
  105.     for (int i = 0; i < strk1.size(); i++) {
  106.         for (int j = i; j > 0 && strk1[j - 1] > strk1[j]; j--) {
  107.             swap(strk1[j - 1], strk1[j]);
  108.             sh++;
  109.         }
  110.     }
  111.     cout << "Время работы программы = " << clock() << endl;
  112.     for (int i = 0; i < strk1.size(); i++) {
  113.         cout << strk1[i] << " ";
  114.     }
  115.     cout << endl;
  116.     cout << "Количество перестановок: " << sh << endl;
  117.     vector <char> strk2;
  118.     strk2 = strk;
  119.     cout << "Сортировка Шелла" << endl;
  120.     int sh1 = 0;
  121.     for (int h = strk2.size() / 2; h > 0; h = h/2) {
  122.         for (int i = 0; i < strk2.size(); i++) {
  123.             for (int j = i + h; j < strk2.size(); j = j + h) {
  124.                 if (strk2[i] > strk2[j]) {
  125.                     swap(strk2[i], strk2[j]);
  126.                     sh1++;
  127.                 }
  128.             }
  129.         }
  130.     }
  131.     cout << "Время работы программы = " << clock() << endl;
  132.     for (int i = 0; i < strk2.size(); i++) {
  133.         cout << strk2[i] << " ";
  134.     }
  135.     cout << endl;
  136.     cout << "Количество перестановок: " << sh1 << endl;
  137.     cout << "Быстрая сортировка" << endl;
  138.     int sh2 = 0;
  139.     vector <char> strk3;
  140.     strk3 = strk;
  141.     for (int i = strk3.size() - 1; i > 0; i--) {
  142.         for (int j = 0; j <= i; j++) {
  143.             if (strk3[j] > strk3[i]) {
  144.                 swap(strk3[j], strk3[i]);
  145.                 sh2++;
  146.             }
  147.         }
  148.     }
  149.     cout << "Время работы программы = " << clock() << endl;
  150.     for (int i = 0; i < strk3.size(); i++) {
  151.         cout << strk3[i] << " ";
  152.     }
  153.     cout << endl;
  154.     cout << "Количество перестановок равно: " << sh2 << endl;
  155.     cout << "Сортировка Таноса" << endl;
  156.     vector <char> strk4;
  157.     strk4 = strk;
  158.     strk4 = Thanos(strk4);
  159.     cout << "Время работы программы = " << clock() << endl;
  160.     for (int i = 0; i < strk4.size(); i++) {
  161.         cout << strk4[i] << " ";
  162.     }
  163.     cout << endl;
  164.     cout << "Количество перестановок равно: " << sh3 << endl;
  165.     return 0;
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement