Java 实现冒泡排序算法:
public class Bubble {
public static void bubble(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = { 2, 1, 3, 7, 6, 4, 5, 9, 8, 0 };
Bubble.bubble(arr, arr.length);
for (int i : arr) {
System.out.print(i + "\t");
}
}
}