Advertisement
Mancolo

Пузырьковая сортировка

Oct 15th, 2022
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void bubbleSort(int list[], int listLength)
  5. {
  6.     while (listLength--)
  7.     {
  8.         bool swapped = false;
  9.  
  10.         for (int i = 0; i < listLength; i++)
  11.         {
  12.             if (list[i] > list[i + 1])
  13.             {
  14.                 swap(list[i], list[i + 1]);
  15.                 swapped = true;
  16.             }
  17.         }
  18.  
  19.         if (swapped == false)
  20.             break;
  21.     }
  22. }
  23.  
  24. int main()
  25. {
  26.     int list[5] = { 3,19,8,0,48 };
  27.     cout << "Input array ..." << endl;
  28.     for (int i = 0; i < 5; i++)
  29.         cout << list[i] << '\t';
  30.     cout << endl;
  31.  
  32.     bubbleSort(list, 5);
  33.  
  34.     cout << "Sorted array ..." << endl;
  35.     for (int i = 0; i < 5; i++)
  36.         cout << list[i] << '\t';
  37.     cout << endl;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement