听说过冒泡排序么?一种很暴力的排序方法。今天我们不希望你用它来排序,而是希望你能算出从小到大冒泡排序的过程中一共进行了多少次数据交换。
输入格式:
第一行为一个正整数 T ,表示有 T 组测试数据。
接下来T行,每行第一个整数N, 然后有N个整数,无序。0<N <= 100。
输出格式:
输出共 T 行。
每行一个整数,代表本行数据从小到大冒泡排序所进行的交换次数。
输入样例:
3
5 1 2 3 4 5
4 5 3 7 1
2 2 1
输出样例:
0
4
1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in) ;
int i,j,m,group,n,temp,count;
int number[]=new int[100];
group=in.nextInt() ;
for(i=0;i<group;i++){
count=0;
n=in.nextInt() ;
for(j=0;j<n;j++){
number[j]=in.nextInt() ;
}
for(j=0;j<n-1;j++){
for(m=0;m<n-j-1;m++){
if (number[m] > number[m+1]) {
count++;
temp=number[m];
number[m]=number[m+1];
number[m+1]=temp;
}
}
}
System.out.print(count);
if(i!=group-1){
System.out.println();
}
}
}
}