package sun;
//从最右边开始,较小的元素一直往左边移动
//最左边的元素为最后的位置
public class Sort {
public static void bubble(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = a.length - 1; j > i; j--) {
if (a[j] < a[j - 1]) {
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = { 9, 8, 4, 3, 2, 1 ,7, 6, 5,};
bubble(arr);
for (int a : arr)
System.out.println(a);
}
}
冒泡排序
最新推荐文章于 2022-04-29 23:24:15 发布