FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaArithmetic/src/sort/BubbleSort.java at master · git-jhy/JavaArithmetic · GitHub
git-jhy
JavaArithmetic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaArithmetic
/
src
/
sort
/
BubbleSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
126 lines (92 loc) · 2.98 KB
Breadcrumbs
JavaArithmetic
/
src
/
sort
/
BubbleSort.java
Copy path
File metadata and controls
126 lines (92 loc) · 2.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package
sort
;
/**
* Created by ozc on 2018/3/14.
*
* @author ozc
* @version 1.0
*/
public
class
BubbleSort
{
public
void
bubble
() {
int
[]
arrays
= {
2
,
5
,
1
,
3
,
4
};
/*//使用临时变量,让两个数互换
int temp;
//第一位和第二位比
if (arrays[0] > arrays[1]) {
//交换
temp = arrays[0];
arrays[0] = arrays[1];
arrays[1] = temp;
}
//第二位和第三位比
if (arrays[1] > arrays[2]) {
temp = arrays[1];
arrays[1] = arrays[2];
arrays[2] = temp;
}
//第三位和第四位比
if (arrays[2] > arrays[3]) {
temp = arrays[2];
arrays[2] = arrays[3];
arrays[3] = temp;
}
//第四位和第五位比
if (arrays[3] > arrays[4]) {
temp = arrays[3];
arrays[3] = arrays[4];
arrays[4] = temp;
}
System.out.println(arrays);
//第一趟排序完成----------------------------
//第一位和第二位比
if (arrays[0] > arrays[1]) {
//交换
temp = arrays[0];
arrays[0] = arrays[1];
arrays[1] = temp;
}
//第二位和第三位比
if (arrays[1] > arrays[2]) {
temp = arrays[1];
arrays[1] = arrays[2];
arrays[2] = temp;
}
//第三位和第四位比
if (arrays[2] > arrays[3]) {
temp = arrays[2];
arrays[2] = arrays[3];
arrays[3] = temp;
}
//第四位不需要和第五位比了,因为在第一趟排序结束后,第五位是最大的了。
System.out.println(arrays);
*/
//装载临时变量
int
temp
;
//记录是否发生了置换, 0 表示没有发生置换、 1 表示发生了置换
int
isChange
;
//记录执行了多少趟
int
num
=
0
;
//外层循环是排序的趟数
for
(
int
i
=
0
;
i
<
arrays
.
length
-
1
;
i
++) {
//每比较一趟就重新初始化为0
isChange
=
0
;
//内层循环是当前趟数需要比较的次数
for
(
int
j
=
0
;
j
<
arrays
.
length
-
i
-
1
;
j
++) {
//前一位与后一位与前一位比较,如果前一位比后一位要大,那么交换
if
(
arrays
[
j
] >
arrays
[
j
+
1
]) {
temp
=
arrays
[
j
];
arrays
[
j
] =
arrays
[
j
+
1
];
arrays
[
j
+
1
] =
temp
;
//如果进到这里面了,说明发生置换了
isChange
=
1
;
}
}
//如果比较完一趟没有发生置换,那么说明已经排好序了,不需要再执行下去了
if
(
isChange
==
0
) {
break
;
}
num
++;
}
System
.
out
.
println
(
num
);
System
.
out
.
println
(
arrays
);
}
}
Back
|
FazBrowse Home
|
New Git URL