Advertisement
MadCortez

Untitled

Jun 2nd, 2021
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9.  
  10. int arr[5];
  11. int n;
  12. int menu();
  13. void bubSort();
  14. void bubSortRec(int size);
  15. void printArr();
  16.  
  17. int main()
  18. {
  19.     setlocale(LC_ALL, "Russian");
  20.     cout << "Введите кол-во элементов массива: ";
  21.     cin >> n;
  22.     cout << "Введите массив: \n";
  23.     for (int i = 0; i < n; i++)
  24.         cin >> arr[i];
  25.     while (true)
  26.     {
  27.         switch (menu())
  28.         {
  29.         case 1: bubSort(); break;
  30.         case 2: bubSortRec(0); break;
  31.         case 3: return 0;
  32.         default: cout << "Выберите опцию, введя число 1-3\n"; continue;
  33.         }
  34.         printArr();
  35.     }
  36. }
  37.  
  38. int menu()
  39. {
  40.     cout << "------------------------------------" << endl;
  41.     cout << "Выберите действие: " << endl;
  42.     cout << "1)Сортировка нерекурсивным методом; " << endl;
  43.     cout << "2)Сортировка рекурсивным методом; " << endl;
  44.     cout << "3)ВЫХОД " << endl;
  45.     int i;
  46.     cin >> i;
  47.     return i;
  48. }
  49.  
  50. void bubSort()
  51. {
  52.     for (int i = 0; i < n - 1; i++)
  53.         for (int j = 0; j < n - i - 1; j++)
  54.             if (arr[j] > arr[j + 1])
  55.                 swap(arr[j], arr[j + 1]);
  56. }
  57.  
  58. void bubSortRec(int size)
  59. {
  60.     for (int i = 0; i < n - size - 1; i++)
  61.         if (arr[i] > arr[i + 1])
  62.             swap(arr[i], arr[i + 1]);
  63.     if (size < n - 1)
  64.         bubSortRec(size + 1);
  65. }
  66.  
  67. void printArr()
  68. {
  69.     for (int i = 0; i < n; i++)
  70.         cout << arr[i] << " ";
  71.     cout << endl;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement