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