大体思想就是通过与相邻元素的比较和交换来逐步将把小的数交换到最前面
public class DemoApplication {
public static void main(String[] args) {
int arr[] = {6, 4, 8, 9, 2, 3, 1};
maopao(arr);
for(int k = 0; k < arr.length; k++){
System.out.print(arr[k] + ",");
}
}
public static void maopao(int arr[]){
int length = arr.length;
//是否有数据交换
boolean flag = true;
for(int i = 0; i < length - 1; i++){
for(int j = 0; j < length - 1; j++){
if(arr[j] > arr[j + 1]){
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = false;
}
}
if(flag){
break;
}
}
}
}
参考地址:排序算法(1) -- 冒泡排序_Dby_freedom的博客-CSDN博客



