基础算法

冒泡排序

public static void main(String[] args) { int ary[] = {20, 90, 60, 100}; //int ary[] = {20, 30, 40, 500}; for (int i = 0; i < ary.length - 1; i++) { //是否是有符合条件的序数据 如果是则不进行多次冒泡排序 boolean flag = true; for (int j = 0; j < ary.length - 1 - i; j++) { int temp; if (ary[j] > ary[j + 1]) { temp = ary[j]; ary[j] = ary[j + 1]; ary[j + 1] = temp; flag = false; } } System.out.println("第" + (i + 1) + "轮"); for (int m = 0; m < ary.length; m++) { System.out.print(ary[m] + "\t"); } if (flag) { break; } }}

    推荐阅读