gashink_t

лаб 17 (готовая) сортировки

Apr 26th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. sort.h:
  2. #pragma once
  3. int* create_hand(int* A, int N); // ручной ввод
  4. int* create_rand(int* A, int N); // случайный ввод
  5. int* create_file(int* A, int* N); // ввод из файла
  6. int bubble(int* A, int N); // метод пузырька
  7. int simple_selection(int* A, int N); // метод простого выбора
  8. int* sort(int* A, int N); // выбор метода сортировки
  9. int partition(int arr[], int low, int high, int& c, int& m); // метод пирамидной осртировки
  10. void quickSort(int arr[], int low, int high, int& c, int& m);// метод пирамидной осртировки
  11. void print(int* A, int N);
  12. lib_sort.cpp:
  13. #include <malloc.h>
  14. #include <algorithm>
  15. #include <iostream>
  16. #include "sort.h"
  17. #define _CRT_SECURE_NO_WARNINGS
  18. using namespace std;
  19.  
  20. int* create_hand(int* A, int N)
  21. {
  22.     A = (int*)malloc(N * sizeof(int));
  23.     for (int i = 0; i < N; i++)
  24.         cin >> A[i];
  25.     return A;
  26. }
  27.  
  28. int* create_rand(int* A, int N)
  29. {
  30.     A = (int*)malloc(N * sizeof(int));
  31.     for (int i = 0; i < N; ++i)
  32.         A[i] = rand() % 100;
  33.     return A;
  34. }
  35.  
  36. int* create_file(int* A, int* N)
  37. {
  38.     *N = 0; // для подсчета кол-ва символов в массиве
  39.     int t = 0;
  40.     char name[20];
  41.     cout << "\nEnter a name file: ";
  42.     cin >> name;
  43.     *N = 0;
  44.     int mas = 0;
  45.     FILE* f;
  46.     errno_t err;
  47.     err = fopen_s(&f, name, "r"); // открытие файла для чтения
  48.     if (err) // проверка открытия файла
  49.     {
  50.         cout << "Error openning file!" << endl;
  51.         A = NULL;
  52.     }
  53.     else {
  54.         for (int i = 0; !feof(f); i++)
  55.         {
  56.             fscanf_s(f, "%d", &mas); // считываем массив с файла и считаем кол-во символов
  57.             *N = *N + 1;
  58.         }
  59.         A = (int*)malloc(*N * sizeof(int));
  60.         rewind(f); // переход на начало файла
  61.         for (int i = 0; !feof(f); i++)
  62.         {
  63.             fscanf_s(f, "%d", &mas); // считываем массив из файла и записываем в массив A
  64.             A[i] = mas;
  65.         }
  66.         fclose(f); // закрытие файла
  67.     }
  68.     return A;
  69. }
  70.  
  71. int bubble(int* A, int N)
  72. {
  73.     cout << "\nThe method of bubble sort!\n" << endl;
  74.     int t, c = 0, m = 0;// c - кол-во сравнений, m - кол-во пересылок
  75.     for (int i = 0; i < N - 1; i++) {
  76.         for (int j = 0; j < N - i - 1; j++) {
  77.             c++;// подсчет сравнений
  78.             if (A[j] > A[j + 1]) {
  79.                 m++; // подсчет пересылок;
  80.                 t = A[j];
  81.                 A[j] = A[j + 1];
  82.                 A[j + 1] = t;
  83.             }
  84.         }
  85.     }
  86.     cout << "Number of comparisons C = " << c << "\nNumber of transfers M = " << m << endl;
  87.     return c + m;
  88. }
  89.  
  90. int simple_selection(int* A, int N)
  91. {
  92.     cout << "\nSimple selection method! " << endl;
  93.     int t, min, c = 0, m = 0;
  94.     for (int i = 0; i < N; i++) {
  95.         min = i;
  96.         for (int j = i + 1; j < N; j++) {
  97.             c++;
  98.             if (A[j] < A[min])
  99.                 min = j; // индекс минимального символа
  100.         }
  101.         m++;
  102.         t = A[i]; //меняем местами
  103.         A[i] = A[min];
  104.         A[min] = t;
  105.     }
  106.     cout << "Number of comparisons C = " << c << "\nNumber of transfers M = " << m << endl;
  107.     return c + m;
  108. }
  109.  
  110. int* sort(int* A, int N)
  111. {
  112.     int c, L = 0;// L - трудоемкость (с+m)
  113.     cout << "\nSelect the sorting method:\n\t1)Bubble selection method;\n\t2)The heapsort algorithm;\n\t3)Simple selection method;\n\t4)Output\nYour select: ";
  114.     cin >> c;
  115.     if (c == 1)
  116.     {
  117.         L = bubble(A, N);
  118.         cout << "\nYour sorted array: ";
  119.         print(A, N);
  120.         cout << "\nThe complexity of the bubble sorting method = " << L << endl;
  121.     }
  122.     if (c == 2)
  123.     {
  124.         int C = 0, M = 0;
  125.         quickSort(A, 0, N - 1, C, M);
  126.         cout << "\nYour sorted array: ";
  127.         print(A, N);
  128.         cout << "Number of comparisons C = " << C << "\nNumber of transfers M = " << M << endl;
  129.         cout << "\nThe complexity of the Hoara sorting method = " << C + M << endl;
  130.     }
  131.     if (c == 3)
  132.     {
  133.         L = simple_selection(A, N);
  134.         cout << "\nYour sorted array: ";
  135.         print(A, N);
  136.         cout << "\nThe complexity of the simple selection sorting method = " << L << endl;
  137.     }
  138.     if (c == 4)
  139.         c = 0;
  140.     cout << endl;
  141.     return A;
  142. }
  143.  
  144. void print(int* A, int N)
  145. {
  146.     for (int i = 0; i < N; i++)
  147.         cout << A[i] << " ";
  148.     cout << endl;
  149. }
  150.  
  151. int partition(int arr[], int low, int high, int& c, int& m)
  152. {
  153.     int pivot = arr[high];    // pivot
  154.     int i = (low - 1);  // Index of smaller element
  155.  
  156.     for (int j = low; j <= high - 1; j++)
  157.     {
  158.         // If current element is smaller than or
  159.         // equal to pivot
  160.         c++;
  161.         if (arr[j] <= pivot)
  162.         {
  163.             i++;    // increment index of smaller element
  164.             m++;
  165.             swap(arr[i], arr[j]);
  166.         }
  167.     }
  168.     m++;
  169.     swap(arr[i + 1], arr[high]);
  170.     return (i + 1);
  171. }
  172.  
  173. /* The main function that implements QuickSort
  174.  arr[] --> Array to be sorted,
  175.   low  --> Starting index,
  176.   high  --> Ending index */
  177. void quickSort(int arr[], int low, int high, int& c, int& m)
  178. {
  179.     c++;
  180.     if (low < high)
  181.     {
  182.         /* pi is partitioning index, arr[p] is now
  183.            at right place */
  184.         int pi = partition(arr, low, high, c, m);
  185.  
  186.         // Separately sort elements before
  187.         // partition and after partition
  188.         quickSort(arr, low, pi - 1, c, m);
  189.         quickSort(arr, pi + 1, high, c, m);
  190.     }
  191. }
  192. lab_17.cpp:
  193. #include <malloc.h>
  194. #include <algorithm>
  195. #include <iostream>
  196. #include "sort.h"
  197. #define _CRT_SECURE_NO_WARNINGS
  198. using namespace std;
  199.  
  200. int main()
  201. {
  202.     int n=0, c = 1; //  Размер массива
  203.     int* a = NULL; // массив чисел
  204.     while (c != 0)
  205.     {
  206.         cout << "Select the method for filling in the array:\n\t1)Enter the array manually;\n\t2)Array of random numbers;\n\t3)Array from the file;\n\t4)End\nYour select: ";
  207.         cin >> c;
  208.         if (c == 1)
  209.         {
  210.             cout << "\nEnter the size of the array: ";
  211.             cin >> n;
  212.             cout << "Enter your array: ";
  213.             a = create_hand(a,n);
  214.             a = sort(a,n);
  215.         }
  216.         if (c == 2)
  217.         {
  218.             cout << "\nEnter the size of the array: ";
  219.             cin >> n;
  220.             a = create_rand(a,n);
  221.             cout << "Your array: ";
  222.             print(a,n);
  223.             a = sort(a,n);
  224.         }
  225.         if (c == 3)
  226.         {
  227.             a = create_file(a,&n);
  228.             if (a != NULL)
  229.             {
  230.                 cout << "Your array: ";
  231.                 print(a,n);
  232.                 a = sort(a,n);
  233.             }
  234.         }
  235.         if (c == 4)
  236.             c = 0;
  237.         cout << endl;
  238.     }
  239.     return 0;
  240. }
Add Comment
Please, Sign In to add comment