FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Problem-solving-with-JavaScript/problem-27.js at master · anasmak04/Problem-solving-with-JavaScript · GitHub
anasmak04
/
Problem-solving-with-JavaScript
Public
forked from
mehediislamripon/Problem-solving-with-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
Problem-solving-with-JavaScript
/
problem-27.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
20 lines (15 loc) · 582 Bytes
Breadcrumbs
Problem-solving-with-JavaScript
/
problem-27.js
Copy path
File metadata and controls
20 lines (15 loc) · 582 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
// binary search implementation
const
arr
=
[
1
,
3
,
5
,
7
,
8
,
9
]
;
const
binarySearch
=
(
arr
,
x
,
start
=
0
,
end
=
arr
.
length
)
=>
{
// If the item does not exist, return -1
if
(
end
<
start
)
return
-
1
;
// Calculate middle index of the array
let
mid
=
Math
.
floor
(
(
start
+
end
)
/
2
)
;
// Is the middle a match?
if
(
arr
[
mid
]
===
x
)
return
mid
;
// Is the middle less than x
if
(
arr
[
mid
]
<
x
)
return
binarySearch
(
arr
,
x
,
mid
+
1
,
end
)
;
// Else the middle is more than x
else
return
binarySearch
(
arr
,
x
,
start
,
mid
-
1
)
;
}
;
console
.
log
(
binarySearch
(
arr
,
9
)
)
;
Back
|
FazBrowse Home
|
New Git URL