Shiyan12

Пузырёк

Dec 22nd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. // СОРТИРОВКА МЕТОДОМ ПУЗЫРЬКА
  5. int main() {
  6.     setlocale(LC_ALL, "rus");
  7.     int const N = 5;
  8.     int a[N];
  9.     srand(time(0));
  10.     cout << " Изначальный массив ";
  11.  
  12.     for (int i = 0; i < N; i++) {
  13.         a[i] = rand() % 21 - 10;
  14.         cout << a[i] << " ";
  15.     }
  16.  
  17.     bool f = true;
  18.     for (int i = N; i > 0 && f; i--) {
  19.         f = false;
  20.         for (int j = 0; j < i - 1; j++)
  21.             if (a[j] > a[j + 1]) {
  22.                 int k = a[j];
  23.                 a[j] = a[j+1];
  24.                 a[j+1] = k;
  25.                 f = true;
  26.             }
  27.     }
  28.  
  29.     cout << endl;
  30.     cout << "Отсортирванный массив ";
  31.  
  32.     for (int i = 0; i < N; i++)
  33.         cout << a[i] << " ";
  34. }
Add Comment
Please, Sign In to add comment