pranjal1805

reversort

Mar 27th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <algorithm>
  3. using namespace std;
  4.    
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10.     int n,p;
  11.     int y=0;
  12.     int a[101];
  13.     cin>>p;
  14.  
  15.     for(int b=0;b<p;b++){
  16.     cin>>n;
  17.    
  18.     for(int i=0;i<n;i++){
  19.         cin>>a[i];               //filling array
  20.     }
  21.    
  22.     int x=0;
  23.    
  24.     for(int i=0;i<n-1;++i)
  25.     {
  26.       int j=i;
  27.         for(int c=i;c<n;++c)
  28.                 if(a[c]<a[j])
  29.                     j=c; //here we find the position which has the min value
  30.    
  31.         int A=i; int B=j;
  32.         while(A<B)
  33.         {
  34.         int temp=a[A];
  35.         a[A]=a[B];
  36.         a[B]=temp;
  37.         ++A;
  38.         --B;
  39.        
  40.     }
  41.  
  42.     x+=j-i+1;
  43.  
  44.     }
  45.    
  46.     cout<<"Case #"<<b+1<<": "<<x<<'\n';
  47.     }
  48.    
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment