public class BubbleSortDome_1 {
static final int SIZE=10;
public static void bubbleSort(int [] a){
int temp;
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < a.length-1; j++) {
//将相邻的两个数据比较,较大的一个往后冒泡
if (a[j]>a[j+1]) {
//交换数据
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
//输出本次结果
System.out.print("第"+i+"步的排序结果:");
for (int k = 0; k < a.length; k++) {
System.out.print(" "+a[k]);
}
System.out.println("");
}
}
public static void main(String[] args) {
int[] buf=new int[SIZE];
//初始化数组
for (int i = 0; i < buf.length; i++) {
buf[i]=(int)(100+Math.random()*(100+1));
}
System.out.println("排序之前的数组为:");
for (int i = 0; i < SIZE; i++) {
System.out.print(buf[i]+" ");
}
//调用方法进行排序
System.out.println("");
bubbleSort(buf);
System.out.println("排序后的数组:");
for (int i = 0; i < buf.length; i++) {
System.out.print(buf[i]+" ");
}
}
}