@Slf4j
public class BubbleSort {
public static int[] bubbleSortSelf(int[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) {
log.info("i:{}", i);
for (int j = 0; j < length - 1 - i; j++) {
log.info("j:{}", j);
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
log.info("bubbleSortSelf: {}", array);
return array;
}
public static void main(String[] args) {
int[] a = { 3, 1, 9, 5, 2, 7 };
bubbleSortSelf(a);
}
外侧临界点,是 length 还是 length-1,应该是length-1,日志如下,最后=length时,内层循环已经不满足条件,停止执行



