冒泡排序

车厢重组

 1 #include<iostream>
 2 #include<cstring>
 3 
 4 using namespace std;
 5 const int N=10005;
 6 
 7 int main(){
 8     //冒泡排序
 9     int a[N],ans=0;
10     cin>>a[0];
11     for(int i=1;i<=a[0];i++)
12         cin>>a[i];
13     int exchange=a[0],bound;
14     while(exchange!=0){
15         bound=exchange;
16         exchange=0;
17         for(int i=1;i<bound;i++){
18             if(a[i]>a[i+1]){
19                 int tmp=a[i];
20                 a[i]=a[i+1];
21                 a[i+1]=tmp;
22                 exchange=i;
23                 ans++;
24             }
25         }
26     }
27     cout<<ans;
28     return 0;
29 }

 

上一篇:SpringBoot Mybatis 注解运行出现奇怪错误:Invalid bound statement (not found)


下一篇:Unity 四叉树