交换排序:2、冒泡排序
前后比较,交换位置
public class BubbleSort {
public void bubbleSort(int[] arrys){
//防止数组下界溢出-1
for(int i=0;i<arrys.length-1;i++){
for(int j=0;j<arrys.length-i-1;j++){
if(arrys[j]>arrys[j+1]) {
int temp = arrys[j];
arrys[j] = arrys[j+1];
arrys[j+1] = temp;
}
}
}
for (int j=0;j<arrys.length;j++){
System.out.println(arrys[j]);
}
}
public static void main(String[] args) {
int[] b=new int[]{1,7,5,72,24,4,2,0};
BubbleSort b1=new BubbleSort();
b1.bubbleSort(b);
}
}