1 public class MaoPaoSort { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 int a[] = { 10, 8, 12, 5, 4 }; 8 int temp; 9 for (int j = 0; j < a.length - 1; j++) { 10 for (int i = 0; i < a.length - 1-j; i++) { 11 //-j是客观事实需要进行的次数,第一次比较4次,第二次比较3次, 12 //以后比较次数都会减少,若不-j也可,但是多余比较没有意义 13 if (a[i] > a[i + 1]) { 14 temp = a[i]; 15 a[i] = a[i + 1]; 16 a[i + 1] = temp; 17 18 } 19 20 } 21 } 22 23 for (int i = 0; i < a.length; i++) { 24 System.out.print(a[i] + ","); 25 } 26 27 } 28 29 }