Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9346757
quickselect.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
quickselect.js
View Options
import
ascending
from
"./ascending.js"
;
// Based on https://github.com/mourner/quickselect
// ISC license, Copyright 2018 Vladimir Agafonkin.
export
default
function
quickselect
(
array
,
k
,
left
=
0
,
right
=
array
.
length
-
1
,
compare
=
ascending
)
{
while
(
right
>
left
)
{
if
(
right
-
left
>
600
)
{
const
n
=
right
-
left
+
1
;
const
m
=
k
-
left
+
1
;
const
z
=
Math
.
log
(
n
);
const
s
=
0.5
*
Math
.
exp
(
2
*
z
/
3
);
const
sd
=
0.5
*
Math
.
sqrt
(
z
*
s
*
(
n
-
s
)
/
n
)
*
(
m
-
n
/
2
<
0
?
-
1
:
1
);
const
newLeft
=
Math
.
max
(
left
,
Math
.
floor
(
k
-
m
*
s
/
n
+
sd
));
const
newRight
=
Math
.
min
(
right
,
Math
.
floor
(
k
+
(
n
-
m
)
*
s
/
n
+
sd
));
quickselect
(
array
,
k
,
newLeft
,
newRight
,
compare
);
}
const
t
=
array
[
k
];
let
i
=
left
;
let
j
=
right
;
swap
(
array
,
left
,
k
);
if
(
compare
(
array
[
right
],
t
)
>
0
)
swap
(
array
,
left
,
right
);
while
(
i
<
j
)
{
swap
(
array
,
i
,
j
),
++
i
,
--
j
;
while
(
compare
(
array
[
i
],
t
)
<
0
)
++
i
;
while
(
compare
(
array
[
j
],
t
)
>
0
)
--
j
;
}
if
(
compare
(
array
[
left
],
t
)
===
0
)
swap
(
array
,
left
,
j
);
else
++
j
,
swap
(
array
,
j
,
right
);
if
(
j
<=
k
)
left
=
j
+
1
;
if
(
k
<=
j
)
right
=
j
-
1
;
}
return
array
;
}
function
swap
(
array
,
i
,
j
)
{
const
t
=
array
[
i
];
array
[
i
]
=
array
[
j
];
array
[
j
]
=
t
;
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Jul 4, 4:26 PM (2 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3268955
Attached To
rDWAPPS Web applications
Event Timeline
Log In to Comment