|
代码如下:
public class bubbleSort {
public static void main(String[] args) {
int arr[] = {5, 6, 2, 9, 4, 0};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length -i - 1; j++) {
if (arr[j] > arr[j + 1]) {
//change their location.
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
for (int element: arr) {
System.out.println(element);
}
}
} |
|