Advertisement
MadCortez

Untitled

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