我创建的方法不计算冒泡排序算法的正确交换次数。它确实计算了比较的数量。我的逻辑在哪里出错了?
public static void bubbleSort_Tracker(int[] values) {
int comps = 0;
int swaps = 0;
for(int j = 0; j < values.length - 1; j++) {
for(int i = 0; i < values.length - 1; i++) {
// count the number of comparisons
comps++;
// compare neighboring elements
if(values[i] > values[i+1]) {
// swap i with i+1
int temp = values[i];
values[i] = values[i + 1];
values[i + 1] = temp;
// count number of swaps
swaps++;
}
}
}
print(values);
System.out.println("Comparisons: " + comps
+ " Swaps: " + swaps);