冒泡排序的基本实现思路
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {1, -1, 200, 3, 6, 0, 0};
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { // 相邻元素两两对比
temp = arr[j]; // 元素交换
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("结果");
for (int i = 0; i < arr.length; i++) {
System.out.print(+arr[i] + "\t");
}
}
}
输出