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 = 1; i < n - 1; i++)
  10. {
  11. int key = arr[i];
  12. int j = i - 1;
  13. comparisons++;
  14. while (j >= 0 && arr[j] > key); {
  15. arr[j+1] = arr[j];
  16. j = j - 1;
  17. }
  18. swaps++;
  19.  
  20. }
  21. cout << "Відсортований масив ";
  22. for (int i = 0; i < n; i++) {
  23. cout << arr[i] << " ";
  24. }
  25. cout << endl;
  26. cout << "Кількість порівняннь: " << comparisons << endl;
  27. cout << "Кількість обмінів: " << swaps << endl;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Відсортований масив 0 0 0 0 0 0 0 9 
Кількість порівняннь: 6
Кількість обмінів: 6