目录
1、冒泡排序概述
冒泡排序的英文Bubble Sort,是一种最基础的交换排序。之所以叫做冒泡排序,因为每一个元素都可以像小气泡一样,根据自身大小一点一点向数组的一侧移动。原理:每一趟只能确定将一个数归位。即第一趟只能确定将末位上的数归位,第二趟只能将倒数第 2 位上的数归位,依次类推下去。如果有 n 个数进行排序,只需将 n-1 个数归位,也就是要进行 n-1 趟操作。而 “每一趟 ” 都需要从第一位开始进行相邻的两个数的比较,将较大的数放后面,比较完毕之后向后挪一位继续比较下面两个相邻的两个数大小关系,重复此步骤,直到最后一个还没归位的数。
2、冒泡排序分类:
冒泡排序1(原始版)、冒泡排序2(递归版1) 、冒泡排序3(递归版2) 、冒泡排序4(优化版)、冒泡排序5(升级版)
3、代码展示和调试
public static void main(String[] args) {
int[] num=new int[]{3,45,89,53,12,19,6,20,75,66,90,30,67,3,1,2,3,4,5,6,68,98,99,76,65,66,33,43,32,43};
int[] num1=new int[]{3,45,89,53,12,19,6,20,75,66,90,30,67,3,1,2,3,4,5,6,68,98,99,76,65,66,33,43,32,43};
int[] num2=new int[]{3,45,89,53,12,19,6,20,75,66,90,30,67,3,1,2,3,4,5,6,68,98,99,76,65,66,33,43,32,43};
int[] num3=new int[]{3,45,89,53,12,19,6,20,75,66,90,30,67,3,1,2,3,4,5,6,68,98,99,76,65,66,33,43,32,43};
int[] num4=new int[]{3,45,89,53,12,19,6,20,75,66,90,30,67,3,1,2,3,4,5,6,68,98,99,76,65,66,33,43,32,43};
System.out.println("冒泡排序1(原始版)");
sort1(num);
System.out.println("冒泡排序2(递归版1)");
sort2(num1);
System.out.println("冒泡排序3(递归版2)");
sort3(num2);
System.out.println("冒泡排序4(优化版)");
sort4(num3);
System.out.println("冒泡排序5(升级版)");
sort5(num4);
}
public static void swap(int[] num,int i,int j){
int c=num[j];
num[j]=num[i];
num[i]=c;
}
冒泡排序1(原始版)
//冒泡排序1
public static void sort1(int[] num){
if(num==null||num.length<2)return;
int count=0;
int count1=0;
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
count1++;
if (num[i] < num[j]) {
swap(num,i,j);
count++;
}
}
for (int m = 0; m < num.length; m++) {
System.out.print(num[m]+",");
}
System.out.println();
}
System.out.println("调用交换函数swap次数"+count);
System.out.println("循环次数"+count1);
}
运行结果:
冒泡排序1(原始版)
99,3,45,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,99,45,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,45,99,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,45,53,99,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,12,45,53,99,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,12,19,45,53,99,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,45,53,99,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,20,45,53,99,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,20,45,53,75,99,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,20,45,53,66,75,99,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,20,45,53,66,75,89,99,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,20,30,45,53,66,75,89,99,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,6,12,19,20,30,45,53,66,67,75,89,99,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
3,3,6,12,19,20,30,45,53,66,67,75,89,99,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
1,3,3,6,12,19,20,30,45,53,66,67,75,89,99,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
1,2,3,3,6,12,19,20,30,45,53,66,67,75,89,99,3,4,5,6,68,90,98,76,65,66,33,43,32,43,
1,2,3,3,3,6,12,19,20,30,45,53,66,67,75,89,99,4,5,6,68,90,98,76,65,66,33,43,32,43,
1,2,3,3,3,4,6,12,19,20,30,45,53,66,67,75,89,99,5,6,68,90,98,76,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,12,19,20,30,45,53,66,67,75,89,99,6,68,90,98,76,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,66,67,75,89,99,68,90,98,76,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,66,67,68,75,89,99,90,98,76,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,66,67,68,75,89,90,99,98,76,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,66,67,68,75,89,90,98,99,76,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,66,67,68,75,76,89,90,98,99,65,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,65,66,67,68,75,76,89,90,98,99,66,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,45,53,65,66,66,67,68,75,76,89,90,98,99,33,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,33,45,53,65,66,66,67,68,75,76,89,90,98,99,43,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,33,43,45,53,65,66,66,67,68,75,76,89,90,98,99,32,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,45,53,65,66,66,67,68,75,76,89,90,98,99,43,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
调用交换函数swap次数196
循环次数900
冒泡排序2(递归版1)
//冒泡排序2
public static void sort2(int[] num){
if(num==null||num.length<2)return;
int count=0;
int count1=0;
for (int i = num.length-1; i >0; i--) {
for (int j = 0; j < i; j++) {
count1++;
if (num[i] < num[j]) {
swap(num,i,j);
count++;
}
}
for (int m = 0; m < num.length; m++) {
System.out.print(num[m]+",");
}
System.out.println();
}
System.out.println("调用交换函数swap次数"+count);
System.out.println("循环次数"+count1);
}
运行结果:
冒泡排序2(递归版1)
3,43,45,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,99,
3,32,43,45,12,19,6,20,53,66,75,30,67,3,1,2,3,4,5,6,68,89,90,76,65,66,33,43,98,99,
3,32,43,43,12,19,6,20,45,53,66,30,67,3,1,2,3,4,5,6,68,75,89,76,65,66,33,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,66,3,1,2,3,4,5,6,67,68,75,76,65,66,89,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,66,3,1,2,3,4,5,6,66,67,68,75,65,76,89,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,65,3,1,2,3,4,5,6,66,66,67,68,75,76,89,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,65,3,1,2,3,4,5,6,66,66,67,68,75,76,89,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,65,3,1,2,3,4,5,6,66,66,67,68,75,76,89,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,65,3,1,2,3,4,5,6,66,66,67,68,75,76,89,90,98,99,
3,32,33,43,12,19,6,20,43,45,53,30,65,3,1,2,3,4,5,6,66,66,67,68,75,76,89,90,98,99,
3,6,32,33,12,19,6,20,43,43,45,30,53,3,1,2,3,4,5,65,66,66,67,68,75,76,89,90,98,99,
3,5,6,32,12,19,6,20,33,43,43,30,45,3,1,2,3,4,53,65,66,66,67,68,75,76,89,90,98,99,
3,4,5,6,12,19,6,20,32,33,43,30,43,3,1,2,3,45,53,65,66,66,67,68,75,76,89,90,98,99,
3,3,4,5,6,12,6,19,20,32,33,30,43,3,1,2,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
2,3,3,4,5,6,6,12,19,20,32,30,33,3,1,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,4,5,6,6,12,19,20,30,32,3,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
调用交换函数swap次数89
循环次数435
冒泡排序3(递归版2)
public static void sort3(int[] num) {
if (num == null || num.length < 2) return;
int count = 0;
int count1 = 0;
for (int i = num.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
count1++;
if (num[j] > num[j + 1]) {
swap(num, j, j + 1);
count++;
}
}
for (int m = 0; m < num.length; m++) {
System.out.print(num[m] + ",");
}
System.out.println();
}
System.out.println("调用交换函数swap次数"+count);
System.out.println("循环次数"+count1);
}
运行结果:
冒泡排序3(递归版2)
3,45,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,99,
3,45,12,19,6,20,53,66,75,30,67,3,1,2,3,4,5,6,68,89,90,76,65,66,33,43,32,43,98,99,
3,12,19,6,20,45,53,66,30,67,3,1,2,3,4,5,6,68,75,89,76,65,66,33,43,32,43,90,98,99,
3,12,6,19,20,45,53,30,66,3,1,2,3,4,5,6,67,68,75,76,65,66,33,43,32,43,89,90,98,99,
3,6,12,19,20,45,30,53,3,1,2,3,4,5,6,66,67,68,75,65,66,33,43,32,43,76,89,90,98,99,
3,6,12,19,20,30,45,3,1,2,3,4,5,6,53,66,67,68,65,66,33,43,32,43,75,76,89,90,98,99,
3,6,12,19,20,30,3,1,2,3,4,5,6,45,53,66,67,65,66,33,43,32,43,68,75,76,89,90,98,99,
3,6,12,19,20,3,1,2,3,4,5,6,30,45,53,66,65,66,33,43,32,43,67,68,75,76,89,90,98,99,
3,6,12,19,3,1,2,3,4,5,6,20,30,45,53,65,66,33,43,32,43,66,67,68,75,76,89,90,98,99,
3,6,12,3,1,2,3,4,5,6,19,20,30,45,53,65,33,43,32,43,66,66,67,68,75,76,89,90,98,99,
3,6,3,1,2,3,4,5,6,12,19,20,30,45,53,33,43,32,43,65,66,66,67,68,75,76,89,90,98,99,
3,3,1,2,3,4,5,6,6,12,19,20,30,45,33,43,32,43,53,65,66,66,67,68,75,76,89,90,98,99,
3,1,2,3,3,4,5,6,6,12,19,20,30,33,43,32,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,33,32,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
调用交换函数swap次数192
循环次数435
冒泡排序4(优化版)
//冒泡排序4
public static void sort4(int[] num){
if(num==null||num.length<2)return;
int count=0;
int count1=0;
for (int i = 0; i < num.length-1; i++) {
boolean flag=true;
for (int j = 0; j < num.length-i-1; j++) {
count1++;
if (num[j+1] < num[j]) {
flag=false;
swap(num,j,j+1);
count++;
}
}
if(flag)break;
for (int m = 0; m < num.length; m++) {
System.out.print(num[m]+",");
}
System.out.println();
}
System.out.println("调用交换函数swap次数"+count);
System.out.println("循环次数"+count1);
}
运行结果:
冒泡排序4(优化版)
3,45,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,99,
3,45,12,19,6,20,53,66,75,30,67,3,1,2,3,4,5,6,68,89,90,76,65,66,33,43,32,43,98,99,
3,12,19,6,20,45,53,66,30,67,3,1,2,3,4,5,6,68,75,89,76,65,66,33,43,32,43,90,98,99,
3,12,6,19,20,45,53,30,66,3,1,2,3,4,5,6,67,68,75,76,65,66,33,43,32,43,89,90,98,99,
3,6,12,19,20,45,30,53,3,1,2,3,4,5,6,66,67,68,75,65,66,33,43,32,43,76,89,90,98,99,
3,6,12,19,20,30,45,3,1,2,3,4,5,6,53,66,67,68,65,66,33,43,32,43,75,76,89,90,98,99,
3,6,12,19,20,30,3,1,2,3,4,5,6,45,53,66,67,65,66,33,43,32,43,68,75,76,89,90,98,99,
3,6,12,19,20,3,1,2,3,4,5,6,30,45,53,66,65,66,33,43,32,43,67,68,75,76,89,90,98,99,
3,6,12,19,3,1,2,3,4,5,6,20,30,45,53,65,66,33,43,32,43,66,67,68,75,76,89,90,98,99,
3,6,12,3,1,2,3,4,5,6,19,20,30,45,53,65,33,43,32,43,66,66,67,68,75,76,89,90,98,99,
3,6,3,1,2,3,4,5,6,12,19,20,30,45,53,33,43,32,43,65,66,66,67,68,75,76,89,90,98,99,
3,3,1,2,3,4,5,6,6,12,19,20,30,45,33,43,32,43,53,65,66,66,67,68,75,76,89,90,98,99,
3,1,2,3,3,4,5,6,6,12,19,20,30,33,43,32,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,33,32,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
调用交换函数swap次数192
循环次数344
冒泡排序5(升级版)
//冒泡排序升级版
public static void sort5(int[] arr){
if(arr==null||arr.length<2)return;
int count=0;
int count1=0;
int Index = 0;
int Border = arr.length - 1;
for (int i = 0; i < arr.length - 1; i++) {
boolean isSorted = true;//有序标记,每一轮的初始是true
for (int j = 0; j < Border; j++) {
count1++;
if (arr[j + 1] < arr[j]) {
isSorted = false;//有元素交换,所以不是有序,标记变为false
swap(arr,j,j+1);
count++;
Index = j;
}
}
for (int m = 0; m < arr.length; m++) {
System.out.print(arr[m]+",");
}
System.out.println();
Border = Index;
//一趟下来是否发生位置交换,如果没有交换直接跳出大循环
if(isSorted ) break;
}
System.out.println("调用交换函数swap次数"+count);
System.out.println("循环次数"+count1);
}
运行结果:
冒泡排序5(升级版)
3,45,53,12,19,6,20,75,66,89,30,67,3,1,2,3,4,5,6,68,90,98,76,65,66,33,43,32,43,99,
3,45,12,19,6,20,53,66,75,30,67,3,1,2,3,4,5,6,68,89,90,76,65,66,33,43,32,43,98,99,
3,12,19,6,20,45,53,66,30,67,3,1,2,3,4,5,6,68,75,89,76,65,66,33,43,32,43,90,98,99,
3,12,6,19,20,45,53,30,66,3,1,2,3,4,5,6,67,68,75,76,65,66,33,43,32,43,89,90,98,99,
3,6,12,19,20,45,30,53,3,1,2,3,4,5,6,66,67,68,75,65,66,33,43,32,43,76,89,90,98,99,
3,6,12,19,20,30,45,3,1,2,3,4,5,6,53,66,67,68,65,66,33,43,32,43,75,76,89,90,98,99,
3,6,12,19,20,30,3,1,2,3,4,5,6,45,53,66,67,65,66,33,43,32,43,68,75,76,89,90,98,99,
3,6,12,19,20,3,1,2,3,4,5,6,30,45,53,66,65,66,33,43,32,43,67,68,75,76,89,90,98,99,
3,6,12,19,3,1,2,3,4,5,6,20,30,45,53,65,66,33,43,32,43,66,67,68,75,76,89,90,98,99,
3,6,12,3,1,2,3,4,5,6,19,20,30,45,53,65,33,43,32,43,66,66,67,68,75,76,89,90,98,99,
3,6,3,1,2,3,4,5,6,12,19,20,30,45,53,33,43,32,43,65,66,66,67,68,75,76,89,90,98,99,
3,3,1,2,3,4,5,6,6,12,19,20,30,45,33,43,32,43,53,65,66,66,67,68,75,76,89,90,98,99,
3,1,2,3,3,4,5,6,6,12,19,20,30,33,43,32,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,33,32,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
1,2,3,3,3,4,5,6,6,12,19,20,30,32,33,43,43,45,53,65,66,66,67,68,75,76,89,90,98,99,
调用交换函数swap次数192
循环次数342