练习:冒泡排序法
1 package com.hanqi; 2 public class BubbleSort 3 { 4 public static void main(String[] args) 5 { 6 //冒泡排序 7 int[]a=new int [] {11,23,9,34,89,70,39,21,1}; 8 9 System.out.print("原始顺序:\t"); 10 for(int t:a) 11 { 12 System.out.print(t+" "); 13 } 14 System.out.println(); 15 16 for(int j=0;j<a.length-1;j++)//循环次数 17 { 18 19 for(int i=0;i<a.length-1-j;i++)//前后比较循环 20 { 21 if(a[i]<a[i+1])// 比较前后元素大小顺序 22 { 23 int b=a[i]; // 临时存放 24 a[i]=a[i+1]; 25 a[i+1]=b; 26 } 27 } 28 System.out.print("第"+(j+1)+"次循环:\t"); 29 for(int t:a) 30 { 31 System.out.print(t+" "); 32 } 33 System.out.println(); 34 } 35 36 } 37 38 }