MahmudulMaruf

Code · Faith · Curiosity

Blog Post

Beeecrowd-1162 - Train Swapping Solution

cpp �� 2022-07-18 13:30:50

cpp

#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; in; 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; jn-1; j++)
    for(i=0; in-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;
}

Output Samples
InputOutput

3
3
1 3 2
4
4 3 2 1
2
2 1
Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.

Post Visited: 92 times

Categories: cpp

2 Comments

  • Avatar

    25-07-2022 12:32:22 PM

    Hello World

    Reply

  • Avatar

    Maruf

    22-07-2022 11:00:37 PM

    Hello World

    Reply

Leave a comment

Captcha