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 243be8f

Browse files
committed
Add comments to binarySearch function.
1 parent 039555f commit 243be8f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

‎src/algorithms/search/binary-search/binarySearch.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ import Comparator from '../../../utils/comparator/Comparator';
1010
*/
1111

1212
export default function binarySearch(sortedArray, seekElement, comparatorCallback) {
13+
// Let's create comparator from the comparatorCallback function.
14+
// Comparator object will give us common comparison methods like equal() and lessThen().
1315
const comparator = new Comparator(comparatorCallback);
1416

17+
// These two indices will contain current array (sub-array) boundaries.
1518
let startIndex = 0;
1619
let endIndex = sortedArray.length - 1;
1720

21+
// Let's continue to split array until boundaries are collapsed
22+
// and there is nothing to split anymore.
1823
while (startIndex <= endIndex) {
24+
// Let's calculate the index of the middle element.
1925
const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2);
2026

2127
// If we've found the element just return its position.
@@ -33,5 +39,6 @@ export default function binarySearch(sortedArray, seekElement, comparatorCallbac
3339
}
3440
}
3541

42+
// Return -1 if we have not found anything.
3643
return -1;
3744
}

0 commit comments

Comments
(0)

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