Se7en's Blog|Learn and live

Java冒泡排序算法

字数统计: 1k阅读时长: 5 min
2017/12/19 Share
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Maopao {
public void bigAndSmall(){
int arr[] = {-1,3,6,7,0,12,35,5,4,666};
/*根据数组的长度循环遍历,并判断如果前一个值小于后一个值,
那么就把“后一个值”排列到“当前前一个值”的前面。*/
for (int i=1;i<arr.length;i++){
System.out.println("当i="+i+"时:");
for (int j=0;j<arr.length-i;j++){
if (arr[j]<arr[j+1]){
int temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
System.out.println("当j="+j+"时:"+arr[0]+","+arr[1]+","+arr[2]+","+arr[3]+","+arr[4]+","+arr[5]+","+arr[6]+","+arr[7]+","+arr[8]+","+arr[9]);
}
System.out.println("FOR循环结果为:"+arr[0]+","+arr[1]+","+arr[2]+","+arr[3]+","+arr[4]+","+arr[5]+","+arr[6]+","+arr[7]+","+arr[8]+","+arr[9]);
System.out.println("____________________________________");
}
for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
public static void main(String args[]){
//main函数中,通过实例化别名点方法名的方式调用并打印出结果
Maopao mp = new Maopao();
mp.bigAndSmall();
}
}

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
当i=1时:
当j=0时:3,-1,6,7,0,12,35,5,4,666
当j=1时:3,6,-1,7,0,12,35,5,4,666
当j=2时:3,6,7,-1,0,12,35,5,4,666
当j=3时:3,6,7,0,-1,12,35,5,4,666
当j=4时:3,6,7,0,12,-1,35,5,4,666
当j=5时:3,6,7,0,12,35,-1,5,4,666
当j=6时:3,6,7,0,12,35,5,-1,4,666
当j=7时:3,6,7,0,12,35,5,4,-1,666
当j=8时:3,6,7,0,12,35,5,4,666,-1
FOR循环结果为:3,6,7,0,12,35,5,4,666,-1
____________________________________
当i=2时:
当j=0时:6,3,7,0,12,35,5,4,666,-1
当j=1时:6,7,3,0,12,35,5,4,666,-1
当j=2时:6,7,3,0,12,35,5,4,666,-1
当j=3时:6,7,3,12,0,35,5,4,666,-1
当j=4时:6,7,3,12,35,0,5,4,666,-1
当j=5时:6,7,3,12,35,5,0,4,666,-1
当j=6时:6,7,3,12,35,5,4,0,666,-1
当j=7时:6,7,3,12,35,5,4,666,0,-1
FOR循环结果为:6,7,3,12,35,5,4,666,0,-1
____________________________________
当i=3时:
当j=0时:7,6,3,12,35,5,4,666,0,-1
当j=1时:7,6,3,12,35,5,4,666,0,-1
当j=2时:7,6,12,3,35,5,4,666,0,-1
当j=3时:7,6,12,35,3,5,4,666,0,-1
当j=4时:7,6,12,35,5,3,4,666,0,-1
当j=5时:7,6,12,35,5,4,3,666,0,-1
当j=6时:7,6,12,35,5,4,666,3,0,-1
FOR循环结果为:7,6,12,35,5,4,666,3,0,-1
____________________________________
当i=4时:
当j=0时:7,6,12,35,5,4,666,3,0,-1
当j=1时:7,12,6,35,5,4,666,3,0,-1
当j=2时:7,12,35,6,5,4,666,3,0,-1
当j=3时:7,12,35,6,5,4,666,3,0,-1
当j=4时:7,12,35,6,5,4,666,3,0,-1
当j=5时:7,12,35,6,5,666,4,3,0,-1
FOR循环结果为:7,12,35,6,5,666,4,3,0,-1
____________________________________
当i=5时:
当j=0时:12,7,35,6,5,666,4,3,0,-1
当j=1时:12,35,7,6,5,666,4,3,0,-1
当j=2时:12,35,7,6,5,666,4,3,0,-1
当j=3时:12,35,7,6,5,666,4,3,0,-1
当j=4时:12,35,7,6,666,5,4,3,0,-1
FOR循环结果为:12,35,7,6,666,5,4,3,0,-1
____________________________________
当i=6时:
当j=0时:35,12,7,6,666,5,4,3,0,-1
当j=1时:35,12,7,6,666,5,4,3,0,-1
当j=2时:35,12,7,6,666,5,4,3,0,-1
当j=3时:35,12,7,666,6,5,4,3,0,-1
FOR循环结果为:35,12,7,666,6,5,4,3,0,-1
____________________________________
当i=7时:
当j=0时:35,12,7,666,6,5,4,3,0,-1
当j=1时:35,12,7,666,6,5,4,3,0,-1
当j=2时:35,12,666,7,6,5,4,3,0,-1
FOR循环结果为:35,12,666,7,6,5,4,3,0,-1
____________________________________
当i=8时:
当j=0时:35,12,666,7,6,5,4,3,0,-1
当j=1时:35,666,12,7,6,5,4,3,0,-1
FOR循环结果为:35,666,12,7,6,5,4,3,0,-1
____________________________________
当i=9时:
当j=0时:666,35,12,7,6,5,4,3,0,-1
FOR循环结果为:666,35,12,7,6,5,4,3,0,-1
____________________________________
666
35
12
7
6
5
4
3
0
-1
CATALOG
  1. 1. 执行结果: