1.1初识冒泡排序
第一次排序:
public class BubbleSortTest {
public static void main(String[] args) {
int[] array = {1, 5, 3, 4, 2, 0};
sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int array[]) {
for (int i = 0; i < array.length-1; i++) {
int temp = 0;
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
控制台输出如下:[1, 3, 4, 2, 0, 5],可见这次排序对5进行了排序。
public class BubbleSortTest {
public static void main(String[] args) {
int[] array = {3,4,2,1,5,6,7,8};
sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int array[]) {
//总共需要排序的次数为array.length - 1
for (int j = 0; j < array.length - 1; j++) {
System.out.format("第 %d 遍:\n", j+1);
for (int i = 0; i < array.length-1-j; i++) {
int temp = 0;
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
System.out.format("第 %d 遍的第%d 次交换:", j+1,i+1);
}
System.out.format("第 %d 遍最终结果:", j+1);
}
}
}
控制台输出:
[1, 5, 3, 4, 2, 0]
[1, 3, 5, 4, 2, 0]
[1, 3, 4, 5, 2, 0]
[1, 3, 4, 2, 5, 0]
[1, 3, 4, 2, 0, 5]
第 1 遍:
第 1 遍的第1 次交换:第 1 遍的第2 次交换:第 1 遍的第3 次交换:第 1 遍的第4 次交换:第 1 遍的第5 次交换:第 1 遍的第6 次交换:第 1 遍的第7 次交换:第 1 遍最终结果:第 2 遍:
第 2 遍的第1 次交换:第 2 遍的第2 次交换:第 2 遍的第3 次交换:第 2 遍的第4 次交换:第 2 遍的第5 次交换:第 2 遍的第6 次交换:第 2 遍最终结果:第 3 遍:
第 3 遍的第1 次交换:第 3 遍的第2 次交换:第 3 遍的第3 次交换:第 3 遍的第4 次交换:第 3 遍的第5 次交换:第 3 遍最终结果:第 4 遍:
第 4 遍的第1 次交换:第 4 遍的第2 次交换:第 4 遍的第3 次交换:第 4 遍的第4 次交换:第 4 遍最终结果:第 5 遍:
第 5 遍的第1 次交换:第 5 遍的第2 次交换:第 5 遍的第3 次交换:第 5 遍最终结果:第 6 遍:
第 6 遍的第1 次交换:第 6 遍的第2 次交换:第 6 遍最终结果:第 7 遍:
第 7 遍的第1 次交换:第 7 遍最终结果:[1, 2, 3, 4, 5, 6, 7, 8]
public class BubbleSortTest {
public static void main(String[] args) {
int[] array = {3,4,2,1,5,6,7,8};
sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int array[]) {
//总共需要排序的次数为array.length - 1
for (int j = 0; j < array.length - 1; j++) {
System.out.format("第 %d 遍:\n", j+1);
boolean flag=true;
for (int i = 0; i < array.length-1-j; i++) {
int temp = 0;
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
flag=false;
}
System.out.format("第 %d 遍的第%d 次交换:", j+1,i+1);
}
if (flag){
break;
}
System.out.format("第 %d 遍最终结果:", j+1);
}
}
}
1.2排序优化
我们可以看出在第四次排序时,就已经排序完成了,所以我们可以进一步优化:
public class BubbleSortTest {
public static void main(String[] args) {
int[] array = {3,4,2,1,5,6,7,8};
sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int array[]) {
//总共需要排序的次数为array.length - 1
for (int j = 0; j < array.length - 1; j++) {
boolean flag = true;
for (int i = 0; i < sortBorder; i++) {
int temp = 0;
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
flag = false;
}
}
if (flag) {
return;
}
}
}
}
第 1 遍:
第 1 遍的第1 次交换:第 1 遍的第2 次交换:第 1 遍的第3 次交换:第 1 遍的第4 次交换:第 1 遍的第5 次交换:第 1 遍的第6 次交换:第 1 遍的第7 次交换:第 1 遍最终结果:第 2 遍:
第 2 遍的第1 次交换:第 2 遍的第2 次交换:第 2 遍的第3 次交换:第 2 遍的第4 次交换:第 2 遍的第5 次交换:第 2 遍的第6 次交换:第 2 遍最终结果:第 3 遍:
第 3 遍的第1 次交换:第 3 遍的第2 次交换:第 3 遍的第3 次交换:第 3 遍的第4 次交换:第 3 遍的第5 次交换:第 3 遍最终结果:第 4 遍:
第 4 遍的第1 次交换:第 4 遍的第2 次交换:第 4 遍的第3 次交换:第 4 遍的第4 次交换:[1, 2, 3, 4, 5, 6, 7, 8]
1.3进一步优化
不难发现,{3,4,2,1,5,6,7,8}这个数组从5开始是已经排好序的,白白比较了那么多次,这就是我们又可以优化的一个点。
public class BubbleSortTest {
public static void main(String[] args) {
//int[] array = {5,8,6,3,9,2,1,7};
int[] array = {3, 4, 2, 1, 5, 6, 7, 8};
sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int array[]) {
//总共需要排序的次数为array.length - 1
int lastExchangeIndex = 0;
int sortBorder = array.length - 1;
for (int j = 0; j < array.length - 1; j++) {
System.out.format("第 %d 遍:\n", j + 1);
boolean flag = true;
for (int i = 0; i < sortBorder; i++) {
int temp = 0;
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
flag = false;
lastExchangeIndex = i;
}
System.out.format("第 %d 遍的第%d 次交换:", j + 1, i + 1);
}
sortBorder = lastExchangeIndex;
if (flag) {
break;
}
System.out.format("第 %d 遍最终结果:", j + 1);
}
}
}
第 1 遍:
第 1 遍的第1 次交换:第 1 遍的第2 次交换:第 1 遍的第3 次交换:第 1 遍的第4 次交换:第 1 遍的第5 次交换:第 1 遍的第6 次交换:第 1 遍的第7 次交换:第 1 遍最终结果:第 2 遍:
第 2 遍的第1 次交换:第 2 遍的第2 次交换:第 2 遍最终结果:第 3 遍:
第 3 遍的第1 次交换:第 3 遍最终结果:第 4 遍:
[1, 2, 3, 4, 5, 6, 7, 8]
从结果可以看出,比较的次数少了很多。