Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c86ccda

Browse files
authored
Update recursive-binary-search.js
1 parent 2d6ce03 commit c86ccda

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

‎recursive-binary-search.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
const binarySearch = (array, searchElement, lowerBound = 0, upperBound) => {
1+
const binarySearch = (array, searchElement, lowerBound, upperBound) => {
2+
lowerBound = lowerBound || 0;
3+
upperBound = upperBound || array.length - 1;
24
if (lowerBound > upperBound) {
35
return false;
46
}
57

6-
const midIndex = Math.floor((lowerBound +upperBound) / 2);
8+
const midIndex = Math.floor(lowerBound +(upperBound-lowerBound) / 2);
79
const midElement = array[midIndex];
810

911
if (searchElement === midElement) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /