一、排序思想
二、图解
三、代码实现
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {21, 9, 31, 4, 8, 3, 12, 7, 9, 11};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
private static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
boolean flag = false;
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
if (!flag) break;
}
}
}原文:https://www.cnblogs.com/luomeng/p/10161794.html