1.每一次循环后,都要找出最大的数据,放到参与比较的这堆数据的最右边 2.拿左边的数字和右边的数字比对,当左边>右边的时候交换位置 public class Arry03 { public static void main(String[] args) { int a[] = {10, 20, 40, 89,80}; for (int i = a.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (a[j] > a[j + 1]) {//交换位置 arr[j]和arr[j+1] int tm; tm = a[j]; a[j] = a[j + 1]; a[j + 1] = tm; } } } for (int i = 0; i < a.length - 1; i++) { System.out.println(a[i]); } } }
冒泡排序实例
最新推荐文章于 2024-05-21 10:23:42 发布