public class BubbleSort {
public static void main(String[] args) {
int[] arr = {3, 4, 1, 3, 2, 8, 1, 0, 2};
bubbleSort(arr);
}
private static void bubbleSort(int[] arr) {
//循环使大的数据放到右边
//arr.length 必须 -1 否则会越界报错
for (int j = 0; j <= arr.length - 1;j++) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
arr[i] = arr[i] ^ arr[i + 1];
arr[i + 1] = arr[i]^ arr[i + 1];
arr[i] = arr[i] ^ arr[i + 1];
}
}
}
print(arr);
}
private static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
02-26
487
11-16
265
11-08
202