FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/sorting/heapSort.java at master · AllAlgorithms/java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
java
Public archive
Notifications
You must be signed in to change notification settings
Fork
83
Star
116
Code
Issues
3
Pull requests
6
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
sorting
/
heapSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (34 loc) · 574 Bytes
Breadcrumbs
java
/
sorting
/
heapSort.java
Copy path
File metadata and controls
39 lines (34 loc) · 574 Bytes
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
public
class
HeapSort
{
public
T
[]
heapSort
(
T
[]
M
) {
int
n
=
M
.
length
-
1
;
for
(
int
i
=
n
/
2
;
i
>=
0
;
i
--) {
heapify
(
i
,
n
,
M
);
}
for
(
int
i
=
n
;
i
>
0
;
i
--) {
T
h1
=
M
[
0
];
T
h2
=
M
[
i
];
M
[
i
] =
h1
;
M
[
0
] =
h2
;
heapify
(
0
,
i
-
1
,
M
);
}
return
M
;
}
public
T
[]
heapify
(
int
i
,
int
r
,
T
[]
M
) {
T
a
=
M
[
i
];
int
j
=
2
*
i
;
while
(
j
<=
r
) {
if
(
j
<
r
&&
M
[
j
+
1
].
compareTo
(
M
[
j
]) >
0
) {
j
++;
}
if
(
a
.
compareTo
(
M
[
j
]) <
0
) {
M
[
i
] =
M
[
j
];
i
=
j
;
j
=
2
*
i
;
}
else
{
j
=
r
+
1
;
}
}
M
[
i
] =
a
;
return
M
;
}
}
Back
|
FazBrowse Home
|
New Git URL