FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Sorts/HeapSortV2.js at master · MMABSOUT/JavaScript · GitHub
MMABSOUT
/
JavaScript
Public
forked from
TheAlgorithms/JavaScript
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
JavaScript
/
Sorts
/
HeapSortV2.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (32 loc) · 770 Bytes
Breadcrumbs
JavaScript
/
Sorts
/
HeapSortV2.js
Copy path
File metadata and controls
42 lines (32 loc) · 770 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
40
41
42
let
arrayLength
=
0
/* to create MAX array */
function
heapRoot
(
input
,
i
)
{
const
left
=
2
*
i
+
1
const
right
=
2
*
i
+
2
let
max
=
i
if
(
left
<
arrayLength
&&
input
[
left
]
>
input
[
max
]
)
{
max
=
left
}
if
(
right
<
arrayLength
&&
input
[
right
]
>
input
[
max
]
)
{
max
=
right
}
if
(
max
!==
i
)
{
swap
(
input
,
i
,
max
)
heapRoot
(
input
,
max
)
}
}
function
swap
(
input
,
indexA
,
indexB
)
{
;
[
input
[
indexA
]
,
input
[
indexB
]
]
=
[
input
[
indexB
]
,
input
[
indexA
]
]
}
export
function
heapSort
(
input
)
{
arrayLength
=
input
.
length
for
(
let
i
=
Math
.
floor
(
arrayLength
/
2
)
;
i
>=
0
;
i
-=
1
)
{
heapRoot
(
input
,
i
)
}
for
(
let
i
=
input
.
length
-
1
;
i
>
0
;
i
--
)
{
swap
(
input
,
0
,
i
)
arrayLength
--
heapRoot
(
input
,
0
)
}
return
input
}
Back
|
FazBrowse Home
|
New Git URL