Vlad5080

Bubble sort

Jan 16th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     vector <int> arr;
  8.     int size;
  9.     int a;
  10.     int temp;
  11.  
  12.     cout << "Введите кол-во элементов\n -> ";
  13.     cin >> size;
  14.  
  15.     if (size <= 0) {
  16.         cerr << "Invalid size" << endl;
  17.         return 1;
  18.     }
  19.  
  20.         cout << " Введите элементы для сортировки\n -> ";
  21.  
  22.     for (int i = 0; i < size; i++) {
  23.         cin >> a;
  24.         arr.push_back(a);
  25.     }
  26.  
  27.    
  28.  
  29.     for (int i = size - 1; i >= 1; i--) {
  30.         for (int j = 0; j < i; j++) {
  31.             if (arr[j] > arr[j + 1]) {
  32.                 temp = arr[j];
  33.                 arr[j] = arr[j + 1];
  34.                 arr[j + 1] = temp;
  35.             }
  36.         }
  37.  
  38.     }
  39.         for (int i = 0; i < size; i++) {
  40.             cout << arr[i] << " ";
  41.         }
  42.         cout << endl;
  43.  
  44.  
  45.         system("pause");
  46.         return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment