Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.72 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. static void RandomArray(int *A, int n) { //случайный массив
  11.     for (int i = 0; i < n; i++)
  12.         A[i] = rand() % 50 + 1;
  13. }
  14.  
  15. static void AscendingArray(int *A, int n) { //возрастающий упорядоченный массив
  16.     for (int i = 0; i < n; i++)
  17.         A[i] = i;
  18. }
  19.  
  20. static void DescendingArray(int *A, int n) { //убывающий упорядоченный массив
  21.     for (int i = n - 1; i >= 0; i--)
  22.         A[i] = i;
  23. }
  24.  
  25.  
  26. static void IndArray(int *Ind, int n) { //заполнение массива индексов
  27.     for (int i = 0; i < n; i++)
  28.         Ind[i] = i;
  29. }
  30.  
  31.  
  32. class Sort {
  33.  
  34. private:
  35.  
  36.     static bool SortCheck(int *A, int n) { //проверка массива на сортировку
  37.         for (int i = 0; i < n-1; i++) {
  38.             if (A[i] > A[i + 1])
  39.                 return false;
  40.         }
  41.         return true;
  42.     }
  43.  
  44.     static bool SortCheckIndex(int *A, int *Ind, int n) { //проверка косвенного массива на сортировку
  45.         for (int i = 0; i < n - 1; i++) {
  46.             if (A[Ind[i]] > A[Ind[i + 1]])
  47.                 return false;
  48.         }
  49.         return true;
  50.     }
  51.  
  52.  
  53.     static void MergeSeries(int *A, int b, int c, int e, int *D) { //слияние серий A[b..c] и A[c+1..e] в массив D[b..e]
  54.         int i = b, j = c + 1, k; //i, j, k хранят номера текущих элементов в двух сериях и массиве D
  55.  
  56.         for (k = b; k <= e; k++)
  57.             if (j > e)
  58.                 D[k] = A[i++];
  59.             else if (i > c)
  60.                 D[k] = A[j++];
  61.             else if (A[i] <= A[j])
  62.                 D[k] = A[i++];
  63.             else D[k] = A[j++];
  64.     }
  65.  
  66.  
  67.     static void SiftHeap(int *A, int i, int m) { //функция проссеивания в пирамиде
  68.         int j = i, k = i * 2 + 1; // k - номер левого сына
  69.         while (k <= m) {
  70.             if (k < m && A[k] < A[k + 1])
  71.                 k++; //больший сын
  72.             if (A[j] < A[k]) {
  73.                 int tmp = A[j];
  74.                 A[j] = A[k];
  75.                 A[k] = tmp;
  76.                 j = k;
  77.                 k = k * 2 + 1;
  78.             }
  79.             else
  80.                 break;
  81.         }
  82.     }
  83.  
  84.  
  85.     static void QuickSortRec(int *A, int *Ind, int b, int e) { //рекурсивный вызов косвенной быстрой сортировки
  86.         int x; //опорный элемент
  87.         int i, j, c = b, d = e;
  88.  
  89.         while (c < d) {
  90.             x = A[Ind[(c + d) / 2]]; i = c; j = d;
  91.             while (i < j) {
  92.                 while (A[Ind[i]] < x) i++;
  93.                 while (A[Ind[j]] > x) j--;
  94.                 if (i <= j) {
  95.                     int tmp = Ind[i];
  96.                     Ind[i] = Ind[j];
  97.                     Ind[j] = tmp;
  98.                     i++;
  99.                     j--;
  100.                 }
  101.             }
  102.  
  103.             if (j - c < d - i) {
  104.                 if (c < j)
  105.                     QuickSortRec(A, Ind, c, j);
  106.                 c = i;
  107.                 }
  108.             else {
  109.                 if (i < d)
  110.                     QuickSortRec(A, Ind, i, d);
  111.                 d = j;
  112.             }
  113.         }
  114.     }
  115.  
  116.  
  117. public:
  118.  
  119.     static void MergeSort(int *A, int n) { //рекуррентное слияние
  120.         unsigned int start_time = clock(); // начальное время
  121.  
  122.         int s, b, c, e; //s - текущая длина серий в массиве, b, c, e - задают границы сливаемых серий
  123.         int *D = new int[n];
  124.  
  125.         for (s = 1; s < n; s *= 2) {
  126.             for (b = 0; b < n; b += s * 2) {
  127.                 c = min(b + s - 1, n - 1);
  128.                 e = min(c + s, n - 1);
  129.                 MergeSeries(A, b, c, e, D);
  130.             }
  131.             for (b = 0; b < n; b++) //копирование отсортированного массива
  132.                 A[b] = D[b];
  133.         }
  134.  
  135.         unsigned int end_time = clock(); // конечное время
  136.         unsigned int search_time = end_time - start_time; // искомое время
  137.         cout << "Performance of the MergeSort is: " << search_time << "ms" << endl;
  138.  
  139.         if (SortCheck(A, n)) //Проверка сортировки
  140.             cout << "Array is sorted" << endl << endl;
  141.         else
  142.             cout << "Array isn't sorted" << endl << endl;
  143.         delete[] D;
  144.     }
  145.  
  146.  
  147.     static void ShellSort(int *A, int *Ind, int n) { //косвенная сортировка Шелла
  148.         unsigned int start_time = clock();
  149.  
  150.         int i, j, h;
  151.         for (h = 1; h <= n / 9; h = h * 3 + 1); //вычисляем начальное значение h (h-цепочки – последовательности элементов с индексами)
  152.         while (h >= 1) {
  153.             for (i = h; i < n; i++)
  154.                 for (j = i - h; j >= 0 && A[Ind[j]] > A[Ind[j + h]]; j -= h) {
  155.                     int tmp = Ind[j];
  156.                     Ind[j] = Ind[j + h];
  157.                     Ind[j + h] = tmp;
  158.                 }
  159.             h = (h - 1) / 3;
  160.         }
  161.  
  162.         unsigned int end_time = clock(); // конечное время
  163.         unsigned int search_time = end_time - start_time; // искомое время
  164.         cout << "Performance of the ShellSort is: " << search_time << "ms" << endl;
  165.  
  166.         for (int i = 0; i < n; i++) {
  167.             cout << A[i] << " " << Ind[i] << endl;
  168.            
  169.         }
  170.  
  171.         if (SortCheckIndex(A, Ind, n)) //Проверка сортировки
  172.             cout << "Array is sorted" << endl << endl;
  173.         else
  174.             cout << "Array isn't sorted" << endl << endl;
  175.     }
  176.  
  177.  
  178.     static void HeapSort(int *A, int n) { //пирамидальная сортировка
  179.         unsigned int start_time = clock(); // начальное время
  180.         int i, m;
  181.  
  182.         for (i = n / 2; i >= 0; i--) //построение пирамиды
  183.             SiftHeap(A, i, n - 1);
  184.  
  185.         for (m = n - 1; m >= 1; m--) { //сортировка массива
  186.             int tmp = A[0];
  187.             A[0] = A[m];
  188.             A[m] = tmp;
  189.             SiftHeap(A, 0, m-1);
  190.         }
  191.  
  192.         unsigned int end_time = clock(); // конечное время
  193.         unsigned int search_time = end_time - start_time; // искомое время
  194.         cout << "Performance of the HeapSort is: " << search_time << "ms" << endl;
  195.  
  196.         if (SortCheck(A, n))
  197.             cout << "Array is sorted" << endl << endl;
  198.         else
  199.             cout << "Array isn't sorted" << endl << endl;
  200.     }
  201.  
  202.  
  203.     static void QuickSort(int *A, int *Ind, int n) { //косвенная быстрая сортировка
  204.         unsigned int start_time = clock(); // начальное время
  205.  
  206.         QuickSortRec(A, Ind, 0, n - 1);
  207.  
  208.         unsigned int end_time = clock(); // конечное время
  209.         unsigned int search_time = end_time - start_time; // искомое время
  210.         cout << "Performance of the QuickSort is: " << search_time << "ms" << endl;
  211.  
  212.         if (SortCheckIndex(A, Ind, n)) //Проверка сортировки
  213.             cout << "Array is sorted" << endl << endl;
  214.         else
  215.             cout << "Array isn't sorted" << endl << endl;
  216.     }
  217. };
  218.  
  219.  
  220. int main()  {
  221.     srand(time(0));
  222.  
  223.     int n;
  224.     cout << "Enter the size of array" << endl;
  225.     cin >> n;
  226.     int *A = new int[n];
  227.     int *Ind = new int[n];
  228.  
  229.     cout << "Merge Sorting Random Array " << endl;
  230.     RandomArray(A, n);
  231.     Sort::MergeSort(A, n);
  232.     cout << "Merge Sorting Ascending Array " << endl;
  233.     AscendingArray(A, n);
  234.     Sort::MergeSort(A, n);
  235.     cout << "Merge Sorting Descending Array " << endl;
  236.     DescendingArray(A, n);
  237.     Sort::MergeSort(A, n);
  238.  
  239.     cout << "Shell Sorting Random Array " << endl;
  240.     RandomArray(A, n);
  241.     IndArray(Ind, n);
  242.     Sort::ShellSort(A, Ind, n);
  243.     cout << "Shell Sorting Ascending Array " << endl;
  244.     AscendingArray(A, n);
  245.     IndArray(Ind, n);
  246.     Sort::ShellSort(A, Ind, n);
  247.     cout << "Shell Sorting Descending Array " << endl;
  248.     DescendingArray(A, n);
  249.     IndArray(Ind, n);
  250.     Sort::ShellSort(A, Ind, n);
  251.  
  252.     cout << "Heap Sorting Random Array " << endl;
  253.     RandomArray(A, n);
  254.     Sort::HeapSort(A, n);
  255.     cout << "Heap Sorting Ascending Array " << endl;
  256.     AscendingArray(A, n);
  257.     Sort::HeapSort(A, n);
  258.     cout << "Heap Sorting Descending Array " << endl;
  259.     DescendingArray(A, n);
  260.     Sort::HeapSort(A, n);
  261.  
  262.     cout << "Quick Sorting Random Array " << endl;
  263.     RandomArray(A, n);
  264.     IndArray(Ind, n);
  265.     Sort::QuickSort(A, Ind, n);
  266.     cout << "Quick Sorting Ascending Array " << endl;
  267.     AscendingArray(A, n);
  268.     IndArray(Ind, n);
  269.     Sort::QuickSort(A, Ind, n);
  270.     cout << "Quick Sorting Descending Array " << endl;
  271.     DescendingArray(A, n);
  272.     IndArray(Ind, n);
  273.     Sort::QuickSort(A, Ind, n);
  274.  
  275.     return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement