MahmudulMaruf

Code · Faith · Curiosity

Blog Post

Beecrwod: 1162 - Train Swapping

cpp �� 2023-08-16 12:39:54

cpp

Problem: 1162 - Train Swapping

Solution: 

#include <bits/stdc++.h>
using namespace std;
int bubble(int a[], int n);
int main()
{
    int a[100], b, c, t, n, i, j, cnt;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(i=0; i<n; i++)
            cin>>a[i];
        bubble(a,n);
    }

    return 0;
}
int bubble(int a[], int n)
{
    int i, j ,k, tmp, cnt=0;
    for(j=0; j<n-1; j++)
    for(i=0; i<n-1; i++)
    {
        if(a[i]>a[i+1])
        {
            tmp=a[i];
            a[i]=a[i+1];
            a[i+1]=tmp;
            cnt++;

        }
    }
    cout<<"Optimal train swapping takes "<<cnt<<" swaps."<<endl;
    
}


Post Visited: 456 times

Categories: cpp

0 Comments

    No comments yet. Be the first to comment!

Leave a comment

Captcha