fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. void bubbleSort(vector<int>& a){
  6. ll x=a.size();
  7. ll swapCount=0;
  8. for(int i=0;i<x-1;i++){
  9. for(int j=0;j<x-1-i;j++){
  10. if(a[j]>a[j+1]){
  11. swap(a[j],a[j+1]);
  12. swapCount++;
  13.  
  14. }
  15.  
  16.  
  17. }
  18. cout<<swapCount<<"\n";
  19. }
  20.  
  21. }
  22.  
  23.  
  24. int main() {
  25. ios_base::sync_with_stdio(0);
  26. cin.tie(0);
  27. ll t;
  28. cin>>t;
  29. vector<int> a(t);
  30. while(t--){
  31. ll n;
  32. cin>>n;
  33. for(int j=0;j<n;j++){
  34. cin>>a[j];
  35. }
  36. bubbleSort(a);
  37. }
  38.  
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5516KB
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
stdout
3
3
3
1
1
1
2
3
3
1
1
1