Guest User

BubbleSort int vector

a guest
Aug 25th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. void BubbleSort(std::vector<int>& vector)
  2. {
  3.     if (vector.size() >= 2)
  4.     {
  5.         bool sorted = false;
  6.  
  7.         while (sorted == false)
  8.         {
  9.             static int swaps = 0;
  10.             swaps = 0;
  11.  
  12.             for (auto i = vector.begin(); i < vector.end() - 1; ++i)
  13.             {
  14.                 if (*i > *(i + 1))
  15.                 {
  16.                     static int cache = *i;
  17.                     cache = *i;
  18.  
  19.                     *i = *(i + 1);
  20.                     *(i + 1) = cache;
  21.                     ++swaps;
  22.                 }
  23.             }
  24.  
  25.             if (swaps == 0)
  26.             {
  27.                 sorted = true;
  28.             }
  29.         }
  30.     }
  31.  
  32.     else
  33.     {
  34.         std::cout << "Vector is too small to bubble sort" << std::endl;
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment