/**
* 冒泡排序
* 思路:在剩余的数组中,两两比较,最后一个为最大,因为像鱼吐泡泡故为冒泡排序
* 时间复杂度:n平方,最好情况:n
* @author ZSL
*/
public class BubboSort {
public static void main(String[] args) {
int[] arrs = {3,5,2,9,1,7,4,6,8};
int len = arrs.length,temp;
boolean isChange ;
System.out.println("原数组:");
print(arrs);
for (int i = 0; i <len ; i++) {
isChange = false;
for (int j = 0; j < len-i-1; j++) {
if (arrs[j] > arrs[j+1]){
temp = arrs[j];
arrs[j] = arrs[j+1];
arrs[j+1] = temp;
isChange = true;
}
}
if (!isChange){
break;
}
}
System.out.println("排序后:");
print(arrs);
}
private static void print(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}