Advertisement
Serafim_

Сортировка пузырьком

Oct 19th, 2017
91
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 <string>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. int main()
  7.  
  8. {
  9.     int n;
  10.     cout << "Введите количество элементов массива : ";
  11.     cin >> n;
  12.     int * a = new int[n];
  13.     for (int i = 0; i < n; i++) {
  14.         cout << "[" << i + 1 << "]" << ": ";
  15.         cin >> a[i];
  16.     }
  17.  
  18.     cout << "Initial : " << endl;
  19.     for(int i = 0; i < n; i++){
  20.         cout << a[i] << "  ";
  21.     }
  22.  
  23.     cout << endl;
  24.  
  25.     for (int i=0; i<n; i++){
  26.         for (int j=0; j<n-1; j++) {
  27.             if (a[j]>a[j+1]){
  28.                 int k=a[j];
  29.                 a[j]=a[j+1];
  30.                 a[j+1]=k;
  31.             }
  32.         }
  33.     }
  34.     for (int i = 0; i < n; i++) {
  35.         cout<<a[i]<<"  ";
  36.     }
  37.     delete a;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement