fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. constexpr int length{ 9 };
  8. int array[length]{ 6, 3, 2, 9, 7, 1, 5, 4, 8 };
  9.  
  10. for (int stIndex{ 0 }; stIndex < length - 1; stIndex++)
  11. {
  12. int lastNum{ stIndex+1 };
  13. int iterTest{ 0 };
  14. for (int index{ 0 }; index < length - lastNum; index++)
  15. {
  16. if (array[index] > array[index + 1])
  17. {
  18. swap(array[index], array[index + 1]);
  19. iterTest++;
  20. }
  21. }
  22. if (iterTest == 0)
  23. break;
  24. }
  25.  
  26. for (int index{ 0 }; index < length; ++index)
  27. {
  28. cout << array[index] << ' ';
  29. }
  30. cout << '\n';
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4564KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9