1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public class alg1 { private static int[] data={9,16,27,23,30,49,21,35}; public static void bubbleSort(){ System.out.println("开始排序"); int arraylength = data.length; for(int i = 0; i<arraylength-1; i++){ boolean flag = false; for (int j = 0; j < arraylength-1 -i ;j++){ if (data[j] - data[j+1] > 0){ int tmp = data[j + 1]; data[j + 1] = data [j]; data[j] = tmp; flag = true; } } System.out.println(java.util.Arrays.toString(data)); if(!flag){ System.out.println("没有进行排序"); } } }
public static void main(String[] args) { bubbleSort(); } }
|