/**
* @param args
*/
public static void main(String[] args) {
int i[] = { 10, 9, 8, 5, 7, 1, 19, 11 };
BubbleSortasc(i);
System.out.println("-----------");
BubbleSortdesc(i);
System.out.println("-----------");
}
/**
* 冒泡排序(指定外层为下标,与内层遍历做比较,每次从外层选出一个与内层全部比较,往前排)
* 升序
*/
public static void BubbleSortasc(int number[]){
int temp=-1;
int size=number.length;
for (int j = 0; j < size-1; j++) {
for (int j2 = j+1; j2 < size; j2++) {
if (number[j2]<number[j]) {
temp=number[j2];
number[j2]=number[j];
number[j]=temp;
}
}
}
for (int i = 0; i < size; i++) {
System.out.print(number[i]+" ");
}
}
/**
* 冒泡排序(常用:外层只做循环遍历,内层循环遍历比较排序,每次选出一个最大或者最小的往后排)
* 降序
*/
public static void BubbleSortdesc(int number[]){
int temp=-1;
for (int i = 0; i < number.length-1; i++) {
for (int j = 0; j < number.length-1-i; j++) {
if (number[j]<number[j+1]) {
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}
for (int i = 0; i < number.length; i++) {
System.out.print(number[i]+" ");
}
}