题目描述
输入N个整数,如果每次只能交换相邻的2个数,要把数组从小到大排序,至少需要交换几次?
输入格式
第一行1个正整数:N,范围在[1,1000]。
第二行N个整数,每个整数范围在[0,1000000]。
输出格式
一行N个从小到大的整数。
输入/输出例子1
输入:
4
5 3 6 1
输出:
4
#include<bits/stdc++.h>
using namespace std;
int n,a[1002],t=0,h=0;
int main(){
cin>>n;
for(int i=1; i<=n; i++)cin>>a[i];
for(int i=1; i<n; i++)
{
for(int j=n; j>1; j--)
{
if(a[j]<a[j-1])
{
h++;
swap(a[j],a[j-1]);
}
}
}
cout<<h;
return 0;
}