Advertisement
AmirVagapov

Untitled

Dec 16th, 2022
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <algorithm>
  4. #include <string>
  5. #include <sstream>
  6. #include <chrono>
  7.  
  8. using namespace std;
  9. using namespace std::chrono;
  10.  
  11. class Array {
  12.     int *a, n;
  13.    
  14.     public:
  15.     // конструктор 1
  16.     // len – число элементов в массиве
  17.     // t = 1 – неупорядоченный массив
  18.     // t = 2 – массив, упорядоченный по неубыванию
  19.     // t = 3 – массив, упорядоченный по невозрастанию
  20.     // d – диапазон псевдослучайных чисел для неупорядоченного массива (при t = 1)
  21.     Array(int len = 1, int d = 10, int t = 1) {
  22.         a = new int[len];
  23.         n = len;
  24.         if (t == 2) {
  25.             a[0] = rand()%d;
  26.             for (int i = 1; i < n; i++) {
  27.                 a[i] = a[i - 1] + rand()%d;
  28.             }
  29.         }
  30.         else if (t == 3) {
  31.             a[n-1] = rand()%d;
  32.             for (int i = n-2; i >= 0; i--) {
  33.                 a[i] = a[i + 1] + rand()%d;
  34.             }
  35.         }
  36.         else {
  37.             for (int i = 0; i < n; i++) {
  38.                 a[i] = rand() % d;
  39.             }
  40.         }
  41.     };
  42.    
  43.      
  44.     Array(int *b, int len) {
  45.         a = new int[len];
  46.         n = len;
  47.         for (int i = 0; i < n; i++) {
  48.             a[i] = b[i];
  49.         }
  50.     }  // конструктор 2: по массиву
  51.     Array(Array &b) {
  52.         a = new int[b.n];
  53.         n = b.n;
  54.         for (int i = 0; i < n; i++) {
  55.             a[i] = b.a[i];
  56.         }
  57.     }
  58.     ~Array() { if(a) delete []a; a = NULL;}
  59.  
  60.     Array& operator = (Array &b) {
  61.         if(this!=(&b)) {
  62.             if (n != b.n) {
  63.                 delete []a;
  64.                 a = new int[b.n];
  65.                 n = b.n;
  66.             }
  67.            
  68.             for (int i = 0; i < n; i++) {
  69.                 a[i] = b.a[i];
  70.             }
  71.         }
  72.         return *this;
  73.     }
  74.    
  75.     int &operator [](int);
  76.      
  77.     bool Test();   // проверка на упорядоченность по неубыванию
  78.     bool operator == (Array);   // равенство элементов массивов (но не порядка)
  79.  
  80.     void Shell_sort();
  81.     void Heapsort();
  82.     void heapify(int, int);
  83.     void Hoar_init();
  84.     void Hoar_sort(int, int);
  85.     void Bit_sort(int, int, int);
  86.     void major_bit();// для Bit_sort
  87.    
  88.     // int getlen() {
  89.     //     return n;
  90.     // }
  91.  
  92.     friend istream & operator >> (istream &, Array &);
  93.     friend ostream & operator << (ostream &, Array &);
  94. };
  95.  
  96. int & Array::operator [](int b) {
  97.     if (b < 0 || b >= n) return a[0];
  98.     return a[b];
  99. }
  100.  
  101. bool Array::Test() {
  102.     for (int i = 1; i < n; i++) {
  103.         if (a[i] < a[i-1]) return false;
  104.     }
  105.     return true;
  106. }
  107.  
  108. bool Array::operator == (Array b) {
  109.     if (n != b.n) return false;
  110.     int len = n;
  111.     for (int i = 0; i < n; i++) {
  112.         bool ch = false;
  113.         for (int j = 0; j < len; j++) {
  114.             if (a[i] == b.a[j]) {
  115.                 ch = true;
  116.                 b[j]=b[len-1];
  117.                 len--;
  118.                 break;
  119.             }
  120.         }
  121.         if (ch == false) return false;
  122.     }
  123.     return true;
  124. }
  125.  
  126. istream & operator >> (istream & in, Array & a) {
  127.     string line;
  128.     getline(cin, line);
  129.     istringstream is(line);
  130.     int size = 1;
  131.     delete [] a.a;
  132.     a.a = new int[size];
  133.     while(is>>a.a[size - 1]) {
  134.         int *temp = new int[size];
  135.         size++;
  136.         for (int i = 0; i < size-1; i++) {
  137.             temp[i] = a.a[i];
  138.         }
  139.         delete [] a.a;
  140.         a.a = new int[size];
  141.         for (int i = 0; i < size-1; i++) {
  142.             a.a[i] = temp[i];
  143.         }
  144.         delete [] temp;
  145.     }
  146.     a.n = size-1;
  147.     return in;
  148. }
  149. ostream & operator << (ostream & out, Array & a) {
  150.     for (int i = 0; i < a.n; i++) {
  151.         out << a[i] << " ";
  152.     }
  153.     return out;
  154. }
  155.  
  156.  
  157.  
  158. void Array::Shell_sort() {
  159.     for (int s = n / 2; s > 0; s /= 2)
  160.         for (int i = s; i < n; ++i){
  161.             int k = a[i];
  162.             int j;
  163.             for (j = i; j >= s && a[j - s] > k; j -= s)
  164.                 a[j] = a[j - s];
  165.             a[j] = k;
  166.         }
  167. }
  168. void Array::Hoar_init() {
  169.     this->Hoar_sort(0,n-1);
  170. }
  171. void Array::Hoar_sort(int low ,int high){
  172.  
  173.     if (low < high) {
  174.         int pivot = a[(low+high)/2];
  175.         int i = low;
  176.         int j = high;
  177.         while (true) {
  178.             while (a[i]<pivot) i++;
  179.             while (a[j]>pivot) j--;
  180.             if (i>j) {
  181.                 break;
  182.             }
  183.             if (a[i]!=a[j]) {
  184.                 a[i]^=a[j];
  185.                 a[j]^=a[i];
  186.                 a[i]^=a[j];
  187.             }
  188.             i++;
  189.             j--;
  190.         }
  191.         cout<<endl<<pivot<<"\t"<<j<<"\t"<<i<<"\n"<<*this<<"\n";
  192.         this->Hoar_sort(low, j);
  193.         this->Hoar_sort(i,high);
  194.     }
  195. }
  196.  
  197. void Array::heapify(int n, int i) {
  198.     int j = 2 * i + 1;
  199.     int x = a[i];
  200.     bool f = true;
  201.     while(j < n && f) {
  202.         if (j + 1 < n && a[j + 1]>a[j]) j++;
  203.         if(a[j] > x) {
  204.             a[i] = a[j];
  205.             i = j;
  206.             j = 2 * i + 1;
  207.         }
  208.         else f = false;
  209.     }
  210.     a[i] = x;
  211. }
  212.  
  213. void Array::Heapsort() {
  214.     for (int i = n / 2 - 1; i >= 0; i--)
  215.         this->heapify(n,i);
  216.     for (int i = n - 1; i > 0; i--) {
  217.         if (a[i]!=a[0]) {
  218.             a[i]^=a[0];
  219.             a[0]^=a[i];
  220.             a[i]^=a[0];
  221.         }
  222.         this->heapify(i, 0);
  223.     }
  224. }
  225. void Array::major_bit() {
  226.     int max = a[0];
  227.     for (int i = 0; i < n; i++) {
  228.         if(a[i]>max) max = a[i];
  229.     }
  230.     int k = 0;
  231.     while(max) {
  232.         max>>=1;
  233.         k++;
  234.     }
  235.     this->Bit_sort(0,n-1,k-1);
  236. }
  237.  
  238. void Array::Bit_sort(int l, int r, int k) {
  239.     if (!(l>=r || k < 0)) {
  240.         int i = l, j = r;
  241.         int mask = 1<<k;
  242.         while (i<=j) {
  243.             while (i<=j && !(a[i]&mask)) i++;
  244.             while (i<=j && a[j]&mask) j--;
  245.             if (i < j) {
  246.                 if (a[i]!=a[j]) {
  247.                     a[i]^=a[j];
  248.                     a[j]^=a[i];
  249.                     a[i]^=a[j];
  250.                 }
  251.                 i++;
  252.                 j--;
  253.             }
  254.         }
  255.     this->Bit_sort(l,j,k-1);
  256.     this->Bit_sort(i,r,k-1);
  257.     }
  258. }
  259. void func_test() {
  260.     Array a(10);
  261.     Array b(a);
  262.     cin>>a;
  263.     cout<<a<<endl<<b<<endl;
  264.     cin>>b;
  265.     Array c = b;
  266.     b.Shell_sort();
  267.     cout<<"After Shell_sort\n"<<b<<endl;
  268.     cout<<"To compare: \n";
  269.     cout<<c<<endl<<b<<endl;
  270.     if (c == b) cout<<"equal"<<endl;
  271.     else cout<<"not equal\n";
  272.    
  273.     Array d1(50,10000);
  274.     Array d2 = d1, d3 = d1;
  275.     cout<<d1<<endl;
  276.     if(!d1.Test()) d1.Hoar_init();
  277.     cout<<d1<<endl;
  278.     if(!d2.Test()) d2.Heapsort();
  279.     cout<<d2<<endl;
  280.     if(!d3.Test()) d3.major_bit();
  281.     cout<<d3<<endl;
  282.    
  283.     Array test1(1000000,100000);
  284.     Array test2 = test1;
  285.     Array test3 = test1;
  286.     Array test4 = test1;
  287.     auto start = high_resolution_clock::now();
  288.     test1.Shell_sort();
  289.     auto stop = high_resolution_clock::now();
  290.     auto duration1 = duration_cast<microseconds>(stop - start);
  291.     start = high_resolution_clock::now();
  292.     test2.Hoar_init();
  293.     stop = high_resolution_clock::now();
  294.     auto duration2 = duration_cast<microseconds>(stop - start);
  295.     start = high_resolution_clock::now();
  296.     test3.Heapsort();
  297.     stop = high_resolution_clock::now();
  298.     auto duration3 = duration_cast<microseconds>(stop - start);
  299.     start = high_resolution_clock::now();
  300.     test4.major_bit();
  301.     stop = high_resolution_clock::now();
  302.     auto duration4 = duration_cast<microseconds>(stop - start);
  303.     if(test1.Test() && test2.Test() && test3.Test() && test4.Test())
  304.         cout<<"Shell_sort: "<<duration1.count()<<"\nHoar_init: "<<duration2.count()<<"\nHeapsort: "<<duration3.count()<<"\nmajor_bit: "<<duration4.count()<<endl
  305.             <<"----------------------------------------------------\n";
  306.    
  307.     start = high_resolution_clock::now();
  308.     test1.Shell_sort();
  309.     stop = high_resolution_clock::now();
  310.     duration1 = duration_cast<microseconds>(stop - start);
  311.     start = high_resolution_clock::now();
  312.     test2.Hoar_init();
  313.     stop = high_resolution_clock::now();
  314.     duration2 = duration_cast<microseconds>(stop - start);
  315.     start = high_resolution_clock::now();
  316.     test3.Heapsort();
  317.     stop = high_resolution_clock::now();
  318.     duration3 = duration_cast<microseconds>(stop - start);
  319.     start = high_resolution_clock::now();
  320.     test4.major_bit();
  321.     stop = high_resolution_clock::now();
  322.     duration4 = duration_cast<microseconds>(stop - start);
  323.    
  324.     cout<<"Shell_sort: "<<duration1.count()<<"\nHoar_init: "<<duration2.count()<<"\nHeapsort: "<<duration3.count()<<"\nmajor_bit: "<<duration4.count()<<endl<<"----------------------------------------------------\n";
  325.    
  326.     Array sequence(1000000,1000,3);
  327.     test1 = sequence;
  328.     test2 = sequence;
  329.     test3 = sequence;
  330.     test4 = sequence;
  331.    
  332.     start = high_resolution_clock::now();
  333.     test1.Shell_sort();
  334.     stop = high_resolution_clock::now();
  335.     duration1 = duration_cast<microseconds>(stop - start);
  336.     start = high_resolution_clock::now();
  337.     test2.Hoar_init();
  338.     stop = high_resolution_clock::now();
  339.     duration2 = duration_cast<microseconds>(stop - start);
  340.     start = high_resolution_clock::now();
  341.     test3.Heapsort();
  342.     stop = high_resolution_clock::now();
  343.     duration3 = duration_cast<microseconds>(stop - start);
  344.     start = high_resolution_clock::now();
  345.     test4.major_bit();
  346.     stop = high_resolution_clock::now();
  347.     duration4 = duration_cast<microseconds>(stop - start);
  348.    
  349.     cout<<"Shell_sort: "<<duration1.count()<<
  350.     "\nHoar_init: "<<duration2.count()<<
  351.     "\nHeapsort: "<<duration3.count()<<
  352.     "\nmajor_bit: "<<duration4.count()<<
  353.     endl<<"----------------------------------------------------\n";
  354. }
  355.  
  356.  
  357. int main() {
  358.     srand(time(0));
  359.     Array a(12,100);
  360.     cout<<a<<endl;
  361.     a.Shell_sort();
  362.     cout<<a<<endl;
  363.    
  364.     // func_test();
  365.    
  366.     return 0;
  367. }
  368.  
  369.  
  370. // Дополнительно (не обязательно).
  371. // 1. Конструктор, который формирует арифметическую прогрессию.
  372. // 2. Перегрузка операции +: слить два упорядоченных массива в третий упорядоченный.
  373. // 3. void operator - (int k); // удалить все вхождения числа k
  374.  
  375.  
  376.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement