= = 复习上冒泡法
= = 复习下冒泡法
public static void bubbleSort(int[] a){ for(int i=0; i<a.length-1; i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]>a[j+1]){ int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } System.out.println(Arrays.toString(a)); }
method 2:
这是 i 与 j 比 所以数组前面的位置排好,不扫描,向后继续。 上面中是 j 与 j+1 比 注意2个方法边界的不同
public static void bubbleSort2(int[] a){ for(int i=0; i<a.length-1; i++){ for(int j=i; j<a.length; j++){ if(a[i]>a[j]){ int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } } System.out.println(Arrays.toString(a)); }