FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
my-java/sort-algorithm/BubbleSort.java at master · Dylan1992/my-java · GitHub
Dylan1992
/
my-java
Public
forked from
xirong/my-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
my-java
/
sort-algorithm
/
BubbleSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (56 loc) · 1.77 KB
Breadcrumbs
my-java
/
sort-algorithm
/
BubbleSort.java
Copy path
File metadata and controls
62 lines (56 loc) · 1.77 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
/**
* description:
* author: xirong
* date: 2015-07-16
* version: 1.0
* copyright 2015 ,all rights reserved.
*/
public
class
BubbleSort
{
// 原始冒泡排序
// 外循环为总共遍历的次数,内循环从最后开始,若小于前者,则往前移动顺序即可
public
static
void
OriginalBubbleSort
(
int
[]
array
){
for
(
int
i
=
1
;
i
<=
array
.
length
;
i
++){
for
(
int
j
=
array
.
length
-
1
;
j
>=
i
;
j
--){
if
(
array
[
j
-
1
]>
array
[
j
]){
Swap
(
array
,
j
-
1
,
j
);
}
}
}
}
// 改善效率的冒泡
// 比如{2,1,3,4,5,6,7,8,9} 第一次循环即可将数组排好顺序,剩下的8次循环是可以不用进行的
public
static
void
ImprovedBubbleSort
(
int
[]
array
){
boolean
loop
=
true
;
for
(
int
i
=
1
;
i
<=
array
.
length
&&
loop
;
i
++){
loop
=
false
;
for
(
int
j
=
array
.
length
-
1
;
j
>=
i
;
j
--){
if
(
array
[
j
-
1
] >
array
[
j
]){
Swap
(
array
,
j
-
1
,
j
);
loop
=
true
;
}
}
}
}
// 交换数组位置
public
static
void
Swap
(
int
[]
array
,
int
start
,
int
end
){
int
temp
=
array
[
start
];
array
[
start
] =
array
[
end
];
array
[
end
] =
temp
;
}
public
static
void
PrintArray
(
int
[]
array
){
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
array
.
length
;
i
++){
sb
.
append
(
array
[
i
]).
append
(
","
);
}
System
.
out
.
println
(
sb
.
toString
());
}
public
static
void
main
(
String
[]
args
) {
int
[]
arr
=
new
int
[]{
8
,
6
,
1
,
4
,
7
,
3
,
9
,
2
,
0
};
System
.
out
.
println
(
"before sort:"
);
PrintArray
(
arr
);
//OriginalBubbleSort(arr);
ImprovedBubbleSort
(
arr
);
System
.
out
.
println
(
"after sort:"
);
PrintArray
(
arr
);
}
}
Back
|
FazBrowse Home
|
New Git URL