Bibodui

ЯМП Лаба 1 2023 (сортировки)

May 9th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. /*
  2. * Создать класс Sort, который будет содержать в себе реализацию сортировок:
  3. 2)выбором;
  4. 4)быструю;
  5.  
  6. Сравнить их работу на различных данных: полностью отсортированные,
  7. отсортированные в обратном порядке, частично отсортированные данные.
  8. */
  9.  
  10. #include <iostream>
  11. #include <Windows.h>
  12. #include <fstream>
  13. #include <ctime>
  14. #include <algorithm>
  15.  
  16. using namespace std;
  17.  
  18. void create_array(int*, int);
  19. void output_array(int*, int);
  20. bool comp(int, int);//компаратор, для функции sort для сортировки в обратном порядке
  21.  
  22. int compare1 = 0;
  23. int compare2 = 0;
  24. int replace1 = 0;
  25. int replace2 = 0;
  26.  
  27. class Sort
  28. {
  29. private:
  30.     int* arr;
  31. public:
  32.     Sort(int* arr2)
  33.     {
  34.         arr = arr2;
  35.     }
  36.     void selection_sort(int size)
  37.     {
  38.         for (int i = 0; i < size - 1; i++)
  39.         {
  40.             int min = i;
  41.             for (int j = i + 1; j < size; j++)
  42.             {
  43.                 compare1++;
  44.                 if (arr[j] < arr[min])
  45.                     min = j;
  46.             }
  47.            
  48.             if (min != i)
  49.             {
  50.                 swap(arr[i], arr[min]);
  51.                 replace1++;
  52.             }
  53.         }
  54.     }
  55.     void quick_Sort(int* arr, int first, int last)
  56.     {
  57.         int mid;
  58.         int f = first, l = last;
  59.         mid = arr[(f + l) / 2]; //вычисление опорного элемента
  60.         do
  61.         {
  62.             while (arr[f] < mid)
  63.             {
  64.                 f++;
  65.                 compare2++;
  66.             }
  67.             while (arr[l] > mid)
  68.             {
  69.                 l--;
  70.                 compare2++;
  71.             }
  72.             compare2 += 2;
  73.             if (f <= l) //перестановка элементов
  74.             {
  75.                 swap(arr[f], arr[l]);
  76.                 replace2++;
  77.                 f++;
  78.                 l--;
  79.             }
  80.         } while (f < l);
  81.         if (first < l) quick_Sort(arr, first, l);
  82.         if (f < last) quick_Sort(arr, f, last);
  83.     }
  84. };
  85.  
  86. int main()
  87. {
  88.     SetConsoleOutputCP(1251);
  89.     SetConsoleCP(1251);
  90.  
  91.     int q;
  92.  
  93.     do
  94.     {
  95.         int size;
  96.         cout << "Введите размер массива:" << endl;
  97.         cin >> size;
  98.  
  99.         int* arr1 = new int[size];
  100.         int* arr2 = new int[size];
  101.         create_array(arr1, size);
  102.         cout << "Исходный массив:" << endl;
  103.         output_array(arr1, size);
  104.  
  105.         for (int i = 0; i < size; i++)
  106.             arr2[i] = arr1[i];
  107.  
  108.         Sort tmp(arr1);
  109.  
  110.         /////////////////////////////////////////////////////////////////////////
  111.  
  112.         sort(arr1, arr1 + size);
  113.         cout << "Отсортированный массив:" << endl;
  114.         output_array(arr1, size);
  115.  
  116.         tmp.selection_sort(size);
  117.         cout << "Работа сортировки выбором на отсортированных данных:" << endl;
  118.         output_array(arr1, size);
  119.  
  120.         tmp.quick_Sort(arr1, 0, size - 1);
  121.         cout << "Работа быстрой сортировки на отсортированных данных:" << endl;
  122.         output_array(arr1, size);
  123.         cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
  124.         cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
  125.  
  126.         compare1 = compare2 = replace1 = replace2 = 0;
  127.  
  128.         /////////////////////////////////////////////////////////////////////////
  129.  
  130.         sort(arr1, arr1 + size, comp);
  131.         cout << "\nОтсортированный в обратном порядке массив:" << endl;
  132.         output_array(arr1, size);
  133.  
  134.         tmp.selection_sort(size);
  135.         cout << "Работа сортировки выбором на отсортированных в обратном порядке данных:" << endl;
  136.         output_array(arr1, size);
  137.  
  138.         sort(arr1, arr1 + size, comp);
  139.  
  140.         tmp.quick_Sort(arr1, 0, size - 1);
  141.         cout << "Работа быстрой сортировки на отсортированных в обратном порядке данных:" << endl;
  142.         output_array(arr1, size);
  143.         cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
  144.         cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
  145.  
  146.         compare1 = compare2 = replace1 = replace2 = 0;
  147.  
  148.         /////////////////////////////////////////////////////////////////////////
  149.  
  150.         sort(arr2, arr2 + size / 2);
  151.         cout << "\nМассив с частично отсортированными данными (половина):" << endl;
  152.         output_array(arr2, size);
  153.  
  154.         for (int i = 0; i < size; i++)
  155.             arr1[i] = arr2[i];
  156.  
  157.         tmp.selection_sort(size);
  158.         cout << "Работа сортировки выбором на частично отсортированных данных:" << endl;
  159.         output_array(arr1, size);
  160.  
  161.         tmp.quick_Sort(arr2, 0, size - 1);
  162.         cout << "Работа быстрой сортировки на частично отсортированных данных:" << endl;
  163.         output_array(arr2, size);
  164.         cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
  165.         cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
  166.  
  167.         cout << "\n0. Выход" << endl;
  168.         cin >> q;
  169.     } while (q != 0);
  170. }
  171.  
  172. void create_array(int* arr, int size)
  173. {
  174.     fstream file_1;
  175.     file_1.open("MyFile", ios::out);
  176.  
  177.     srand(static_cast<int>(time(0)));
  178.  
  179.     for (int i = 0; i < size; i++)
  180.     {
  181.         file_1 << rand() % 100;
  182.         file_1 << ' ';
  183.     }
  184.     file_1.close();
  185.  
  186.     file_1.open("MyFile", ios::in);
  187.     for (int i = 0; i < size; i++)
  188.         file_1 >> arr[i];
  189.  
  190.  
  191.     file_1.close();
  192. }
  193.  
  194. void output_array(int* arr, int size)
  195. {
  196.     for (int i = 0; i < size; i++)
  197.     {
  198.         cout << arr[i] << '\t';
  199.         if (!((i + 1) % 15))
  200.             cout << endl;
  201.     }
  202.     cout << endl;
  203. }
  204.  
  205. bool comp(int first, int second)
  206. {
  207.     return first > second;
  208. }
Add Comment
Please, Sign In to add comment