- import java.util.*;
- class main{
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- int count = sc.nextInt();
- for(int i =0;i<count;i++){
- int[] N = new int[sc.nextInt()];
- for(int j =0;j<N.length;j++)
- N[j]= sc.nextInt();
- BubbleSort(N);
- }
- }
- static void BubbleSort(int[] N){
- int count =0;
- for(int i = N.length-1;i>0;i--)
- for(int j = 0;j<i;j++)
- if(N[j+1]<N[j]){
- int tmp = N[j+1] ;
- N[j+1] = N[j];
- N[j] = tmp ;
- count++;
- }
- System.out.println("Optimal train swapping takes "+count+" swaps.");
- }
- }
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。