//冒泡排序
//冒泡排序要经过N-1步的中间排序才能排完,效率较低
public class BublleSort {
public static void main(String[] args) {
int[] arr = {1, 56, 36, 74, 2, 6, 3, 21, 5};
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
//相比较然后两数交换
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.print("排序过后的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(" " + arr[i]);
}
}
}
排序过后的数组: 1 2 3 5 6 21 36 56 74
原文:https://www.cnblogs.com/jasonboren/p/10778787.html