package com.hanqi; public class Test15 { public static void main(String[] args) { // 冒泡排序 從小到大 int[] a = new int[] { 23, 45, 67, 12, 97, 78, 36, 8 }; System.out.print("原始順序:"); for (int t : a) { System.out.print(t + " "); } System.out.println(); int m = 0; //循環次數 0 - 6 for (int j = 0; j < a.length - 1; j++) { //前后比較循環 0 - 6 for (int i = 0; i < a.length - 1 - j; i++) { // 比較前后元素的大小順序 if (a[i] > a[i + 1]) { // 臨時存放 int b = a[i]; a[i] = a[i + 1]; a[i + 1] = b; } m++; } System.out.print( (j+1) + " 次循環:"); for (int t : a) { System.out.print(t + " "); } System.out.println(); } System.out.println("m = " + m); } }