FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Search/InterpolationSearch.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
/
Search
/
InterpolationSearch.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (34 loc) · 920 Bytes
Breadcrumbs
JavaScript
/
Search
/
InterpolationSearch.js
Copy path
File metadata and controls
39 lines (34 loc) · 920 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
/**
* Interpolation Search
*
* Time Complexity:
* -Best case: O(1)
* -Worst case: O(n)
* -O((log(log(n))) If the data are uniformly distributed
*
*
*/
export
function
interpolationSearch
(
arr
,
key
)
{
const
length
=
arr
.
length
-
1
let
low
=
0
let
high
=
length
let
position
=
-
1
let
delta
=
-
1
// Because the array is sorted the key must be between low and high
while
(
low
<=
high
&&
key
>=
arr
[
low
]
&&
key
<=
arr
[
high
]
)
{
delta
=
(
key
-
arr
[
low
]
)
/
(
arr
[
high
]
-
arr
[
low
]
)
position
=
low
+
Math
.
floor
(
(
high
-
low
)
*
delta
)
// Target found return its position
if
(
arr
[
position
]
===
key
)
{
return
position
}
// If the key is larger then it is in the upper part of the array
if
(
arr
[
position
]
<
key
)
{
low
=
position
+
1
// If the key is smaller then it is in the lower part of the array
}
else
{
high
=
position
-
1
}
}
return
-
1
}
Back
|
FazBrowse Home
|
New Git URL