1.思路:依次对比2个相邻数,满足排序条件(第一个比第二个数大)则交换位置
2.时间复杂度
O(n^2)
3.空间复杂度
O(1)
4.动图演示
5.代码展示
package chapter05;
public class BubbleSort {
public static void main(String[] args) {
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
bubbleSort(array);
System.out.println(Arrays.toString(array));//把数组元素转化成字符串形式直接输出
}
public static void bubbleSort(int[] array) {
if (array == null || array.length <= 1) {
return;
} else
{
// 外层循环控制循环的次数
for (int i = 0; i < length - 1; i++) {
// 内层循环控制每一轮比较次数,每进行一轮排序都会找出一个较大值
for (int j = 0; j < length - 1 - i; j++) {
// 前面的数大于后面的数就进行交换
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
return;
}
}
}



