冒泡排序

public class MM {public static void main(String[] args) {
int[] arr = new int[] { 2, 8, 7, 9, 4, 1, 5, 0 };
bubbleSort(arr);
}

public static void bubbleSort(int[] arr) {
//控制多少轮
for (int i = 1; i < arr.length; i++) {
//控制每一轮的次数
for (int j = 0; j <= arr.length -1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));

}
}
posted @ 2021-01-18 16:04  阿白i  阅读(44)  评论(0)    收藏  举报