FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/src/test/java/sort/basic/InsertSort.java at master · verifyCode/algorithms · GitHub
verifyCode
algorithms
Repository navigation
Code
Issues
Pull requests
2
(2)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
src
/
test
/
java
/
sort
/
basic
/
InsertSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (40 loc) · 1.12 KB
Breadcrumbs
algorithms
/
src
/
test
/
java
/
sort
/
basic
/
InsertSort.java
Copy path
File metadata and controls
47 lines (40 loc) · 1.12 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
package
sort
.
basic
;
import
com
.
sort
.
SortTestHelper
;
import
java
.
util
.
Arrays
;
/**
* @author xjn
* @since 2020-05-07
*/
public
class
InsertSort
{
public
static
void
sort
(
int
[]
array
) {
for
(
int
i
=
1
;
i
<
array
.
length
;
i
++) {
for
(
int
j
=
i
;
j
>
0
;
j
--) {
if
(
array
[
j
-
1
] >
array
[
j
]) {
SortTestHelper
.
swap
(
array
,
j
,
j
-
1
);
}
else
{
break
;
}
}
}
}
public
static
void
sort2
(
int
[]
array
) {
for
(
int
i
=
1
;
i
<
array
.
length
;
i
++) {
int
element
=
array
[
i
];
int
j
;
for
(
j
=
i
;
j
>
0
;
j
--) {
if
(
array
[
j
-
1
] >
element
) {
array
[
j
] =
array
[
j
-
1
];
}
else
{
break
;
}
}
array
[
j
] =
element
;
}
}
public
static
void
main
(
String
[]
args
) {
int
[]
ints
=
SortTestHelper
.
generateRandomArray
(
10
,
1
,
10
);
System
.
out
.
println
(
Arrays
.
toString
(
ints
));
sort2
(
ints
);
System
.
out
.
println
(
Arrays
.
toString
(
ints
));
}
}
Back
|
FazBrowse Home
|
New Git URL