> Bubble `BUUBLE
> Sorting
**SWAPING**
`
import java.util.Random;
public class bubble {
**Initializing**
public static int count1 = 0;
public static int swap = 0;
public static int[][] Bubbles(int[] a) {
int output[] = new int[2];
for (int pass = 0; pass < a.length; pass++) {
for (int i = 0; i < a.length - 1; i++) {
count1++;
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swap++;
}
}
}
output[0] = swap;
output[1] = count1;
return new int[][] {
a,
output
};
}
}
Bubble sort **Bubble**
&#13;
答案 0 :(得分:0)
这取决于您的输入数组是按升序还是降序排序。如果你的输入数组是这样排序的:{10,9,8,...}那么a [0]&gt; a [1],你将在嵌套的for循环中输入if-then结构,增加swap。因此,交换将具有非零值。