目录
概念
代码实现
public class BubbleSort {
public static void main(String[] args) {
//int[] ints = {3, -1, 7, 0, 9, -4};
int[] ints = {3, 4, 7, 8, 12, 11};
int temp = 0;
boolean isChange = false;
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints.length - 1 - i; j++) {
if(ints[j] > ints[j+1]){
isChange = false;
temp = ints[j];
ints[j] = ints[j+1];
ints[j+1] = temp;
}
}
if (isChange){
//如果有一行没有执行转置操作的话, 代表已经排好序, 不需要继续排序, 直接退出
for (int j = 0; j < ints.length; j++) {
System.out.print(ints[j] +" ");
}
System.out.println("!!!!");
break;
}else {
System.out.println("!!!!");
isChange = true;
}
}
}
}