fork download
  1. #include <iostream>
  2. using namespace std;
  3. int main () {
  4. int arr[] = {0, 1, 2, 3, 4, 5, 8, 9};
  5. int n=8;
  6.  
  7. int comparisons = 0;
  8. int swaps = 0;
  9. for (int i = 0; i < n; i++) {
  10. for (int j = 0; j < n - i - 1; j++) {
  11.  
  12. comparisons++;
  13.  
  14. if (arr[j] > arr[j+1]) {
  15. int x = arr[j];
  16. arr[j] = arr[j + 1];
  17. arr[j + 1] = x;
  18.  
  19. swaps++;
  20.  
  21. }
  22. }
  23. }
  24. cout << "Відсортований масив ";
  25. for (int i = 0; i < n; i++) {
  26. cout << arr[i] << " ";
  27. }
  28. cout << endl;
  29. cout << "Кількість порівняннь: " << comparisons << endl;
  30. cout << "Кількість обмінів: " << swaps << endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Відсортований масив 0 1 2 3 4 5 8 9 
Кількість порівняннь: 28
Кількість обмінів: 0