avr39ripe

cppSortBubleDemo

Jul 19th, 2021
978
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. int main()
  5. {
  6.     const int randBegin{ 10 };
  7.     const int randEnd{ 99 };
  8.  
  9.     const int arrSize{ 10 };
  10.  
  11. //Ctrl+ K + C - comment
  12. //Ctrl + K + U - uncomment
  13.  
  14.     int arr[arrSize]{ 6,1,4,2,8,9,11,3,2,1 };
  15.     //int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
  16.     //int arr[arrSize]{};
  17.  
  18.     int copy{};
  19.     //char arr[arrSize]{ 'a','w','f','z','q','l','a','p','a','c' };
  20.     //char copy{};
  21.  
  22.     //srand(time(0));
  23.     //for (int i{ 0 }; i < arrSize; ++i) { arr[i] = randBegin + (rand() % (randEnd - randBegin)); }
  24.  
  25.     for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << '\t'; } std::cout << '\n';
  26.  
  27.     for (int head{ 0 }; head < arrSize; ++head)
  28.     {
  29.         for (int tail{ arrSize - 1 }; tail > head; --tail)
  30.         {
  31.             if (arr[tail] < arr[tail - 1])
  32.             {
  33.                 copy = arr[tail-1];
  34.                 arr[tail - 1] = arr[tail];
  35.                 arr[tail] = copy;
  36.  
  37.                 for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << '\t'; } std::cout << " head: " << head << '\n';
  38.             }
  39.         }
  40.     }
  41.  
  42.     for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << '\t'; } std::cout << '\n';
  43.  
  44.     return 0;
  45. }
RAW Paste Data