avr39ripe

cppSortInsertDemo

Jul 19th, 2021
1,007
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 insertPos{ 1 }; insertPos < arrSize; ++insertPos)
  28.     {
  29.         for (int testInsPos{insertPos}; testInsPos > 0; --testInsPos)
  30.         {
  31.             if (arr[testInsPos] < arr[testInsPos - 1])
  32.             {
  33.                 copy = arr[testInsPos];
  34.                 arr[testInsPos] = arr[testInsPos -1];
  35.                 arr[testInsPos - 1] = copy;
  36.  
  37.                 for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << '\t'; } std::cout << '\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