fork(2) download
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int n;
  5. bool check(int *arr) {
  6. for(int u=1; u<n; u++) {
  7. if (arr[u]<arr[u-1]) return 0;
  8. }
  9. return 1;
  10. }
  11. int main() {
  12. cin >> n;
  13. int arr[n];
  14. for(int i=0; i<n; i++) cin >> arr[i];
  15. int counter=0;
  16. while(!check(arr)) {
  17. int pos = 1;
  18. for(int j=pos; j<n; j++) {
  19. if(arr[j]<arr[j-1]) {
  20. int prev;
  21. prev = arr[j-1];
  22. arr[j-1] = arr[j];
  23. arr[j] = prev;
  24. pos = j;
  25. counter++;
  26. }
  27. }
  28. }
  29. cout << counter;
  30. return 0;
  31. }
Success #stdin #stdout 0s 4456KB
stdin
3
1 3 2
stdout
1