时间复杂度:平均O(n²) 最好O(n) 最坏O(n²)
空间复杂度:O(1)
稳定性:稳定
特点:n小时较好
public class BubbleSort {
public static void main(String[] args) {
int[] a = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 };
bubbleSort(a);
for (int n : a) {
System.out.print(n + " ");
}
}
public static void bubbleSort(int[] a) {
if (a == null) {
return;
}
int i, j, temp;
int l = a.length;
for (i = 0; i < l - 1; i++) {
for (j = l - 1; j > i; j--) {
if (a[j] < a[j - 1]) {
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
}