GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add comments to binarySearch function. · BrandonWeb/javascript-algorithms@243be8f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/algorithms/search/binary-search
    • binarySearch.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/algorithms/search/binary-search/binarySearch.js
+7Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.