1 /**
2 * @deprecated :冒泡排序
3 */
4 public class Demo03 {
5 public static void main(String[] args) {
6 int [] a ={1 ,56 ,5 ,63 ,9 ,4 ,23 ,96};
7 int[] b= sort(a);
8 System.out.println(Arrays.toString(b));
9
10 }
11 public static int[] sort(int[] array){
12 int temp =0; //临时变量
13 boolean flag =false;
14 for (int i = 0; i < array.length-1; i++) {
15 //内层循环,判断两个数,如果第一个数大于第二个数,交换位置
16 for (int j = 0; j < array.length-1-i; j++) {
17 if (array[j+1]<array[j]){
18 temp =array[j];
19 array[j]= array[j+1];
20 array[j+1] =temp;
21 flag=true;
22 }
23 }
24 if(flag==false){
25 break;
26 }
27 }
28 return array;
29 }
30 }
31 //例
32 //{1 ,56 ,5 ,63 ,9 ,4 ,23 ,96}
33 //i=0后进入内循环,直到将最大的数放在最后一位
34 //{1,56,5,63,9,4,23,96}
35 //{1,5,56,63,9,4,23,96}
36 //{1,5,56,63,9,4,23,96}
37 //...直到内循环结束,将96放在最后一位
//i=1