FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Sorts/ShellSort.java at master · java66liu/Java · GitHub
java66liu
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Sorts
/
ShellSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (34 loc) · 1.01 KB
Breadcrumbs
Java
/
Sorts
/
ShellSort.java
Copy path
File metadata and controls
41 lines (34 loc) · 1.01 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
package
Sorts
;
import
static
Sorts
.
SortUtils
.*;
public
class
ShellSort
implements
SortAlgorithm
{
/**
* This method implements Generic Shell Sort.
*
* @param array the array to be sorted
*/
@
Override
public
<
T
extends
Comparable
<
T
>>
T
[]
sort
(
T
[]
array
) {
int
length
=
array
.
length
;
int
gap
=
1
;
/* Calculate gap for optimization purpose */
while
(
gap
<
length
/
3
) {
gap
=
3
*
gap
+
1
;
}
for
(;
gap
>
0
;
gap
/=
3
) {
for
(
int
i
=
gap
;
i
<
length
;
i
++) {
int
j
;
for
(
j
=
i
;
j
>=
gap
&&
less
(
array
[
j
],
array
[
j
-
gap
]);
j
-=
gap
) {
array
[
j
] =
array
[
j
-
gap
];
}
array
[
j
] =
array
[
i
];
}
}
return
array
;
}
/* Driver Code */
public
static
void
main
(
String
[]
args
) {
Integer
[]
toSort
= {
4
,
23
,
6
,
78
,
1
,
54
,
231
,
9
,
12
};
ShellSort
sort
=
new
ShellSort
();
print
(
sort
.
sort
(
toSort
));
}
}
Back
|
FazBrowse Home
|
New Git URL