Running Time of Algorithms

  • + 0 comments
    #include <bits/stdc++.h>
    using namespace std;
    int main(){
        int n, i;
        cin>>n;
        vector<int>v(n), v2(n); 
        int count = 0;
        for (i=0;i<n;i++){
            cin >> v[i];
            v2[i] = v[i];
        }
        sort(v2.begin(),v2.end());
        while (v != v2){
            for (i=0;i<n;i++){
                if (v[i] > v[i+1]){
                    swap(v[i],v[i+1]);
                    count++;
                    break;
                }
            } 
        }  
        cout << count;
        return 0;
    }