Aizu ALDS1_2_A Bubble Sort(冒泡排序)

来源:互联网 发布:腾讯微云网盘for mac 编辑:程序博客网 时间:2024/05/01 21:17
#include <bits/stdc++.h>#define _ ios_base::sync_with_stdio(0);cin.tie(0);#define INF 0x3f3f3f3f#define eps 1e-5typedef long long LL;const double pi = acos(-1.0);const long long mod = 25 * 1E8;using namespace std;int a[200005];int main(){    //freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    ios_base::sync_with_stdio(0);cin.tie(0);    int n;    cin >> n;    int ans = 0;    for(int i = 0;i < n;i++)        cin >> a[i];    for(int i = 0;i < n;i++)        for(int j = n - 1;j >= i + 1;j--)            if(a[j] < a[j - 1])                swap(a[j],a[j - 1]),ans++;    for(int k = 0;k < n - 1;k++)        printf("%d ",a[k]);    printf("%d\n",a[n - 1]);    printf("%d\n",ans);    return 0;}
0 0
原创粉丝点击