/**
* @param args
*/
公共靜態void main(String[] args) {
int[] a = { 12,2,45,23,9,88,33,23,22,5,4,4,5,1,9,7,2,
7, 8, 0 };
array sort . bubble sort(a);
}
/**
*冒泡排序。從大到小排序。& ltbr & gt
*
* @param源
* @返回
*/
public static int[]bubble sort(int[]source){
boolean isSort = false//排序或不排序
for(int I = 1;我& ltsource .長度;i++) {
isSort = false//每次排序前初始化為false。
for(int j = 0;j & ltsource.length - ij++) {
if(source[j]& lt;source[j + 1]) {
int temp = source[j];
源[j] =源[j+1];
source[j+1]= temp;
isSort = true//TRUE表示這個循環(外循環)有排序。
}
}
如果(!isSort)
打破;//如果沒有排序,則數據已經排序。
//輸出每個子循環的排序數組中的元素。
printArray(source,I);
}
返回來源;
}
/**
*循環數組中的元素。
*
* @param a
* @param idx
*,第壹個周期的索引。
*/
public static void print array(int[]a,int idx) {
for(int I = 0;我& lta .長度;i++) {
System.out.print(a[i] + (i!= a.length-1?",":""));
}
system . out . println("-idx:"+idx);
}
}