fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. void bubble_sort(vector<int>& a){
  6. int n= a.size();
  7. int swapCount= 0;
  8. for(int i=0;i<n-1;i++){
  9. for(int j=0;j<n-1-i;j++){
  10. if(a[j]>a[j+1]){
  11. swap(a[j],a[j+1]);
  12. swapCount++;
  13. }
  14. }
  15. }
  16. cout<<swapCount<<"\n";
  17. }
  18.  
  19. int main() {
  20. // your code goes here
  21. ios_base::sync_with_stdio(0);
  22. cin.tie(0);
  23. int t;cin >> t;
  24. while(t--){
  25. int n;
  26. cin>>n;
  27. vector<int>a(n);
  28. for(int i=0;i<n;i++) cin>>a[i];
  29. bubble_sort(a);
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5476KB
stdin
4
8
176 -272 -272 -45 269 -327 -945 176 
2
-274 161
7
274 204 -161 481 -606 -767 -351
2
154 -109-i-i
stdout
15
0
16
1