Bibodui

Лаба 1 (2)

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