gashink_t

лаба 17 сортировки

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