冒泡排序算法
每次循环结束后找出这堆数据的最大值,然后将其放在最右边
核心拿左边的数据和右边的数据作比较,当左边的数据大于右边的数据时,进行交换
代码:
public class MaoPaoSort {
public static void main(String[] args) {
int [] a={5,6,4,8,9,0,10,11};
for (int i = a.length-1; i >=0 ; i--) {
for (int j = 0; j < i; j++) {
if (a[j]>a[j+1]){
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for (int i = 0; i <a.length ; i++) {
System.out.println(a[i]);
}
}
}