From 243be8f2d180ab781af0d4c25becb6a9f3ff25f1 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Tue, 4 Dec 2018 08:04:24 +0200 Subject: [PATCH] Add comments to binarySearch function. --- src/algorithms/search/binary-search/binarySearch.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/algorithms/search/binary-search/binarySearch.js b/src/algorithms/search/binary-search/binarySearch.js index 8fe622db..b9e18aab 100644 --- a/src/algorithms/search/binary-search/binarySearch.js +++ b/src/algorithms/search/binary-search/binarySearch.js @@ -10,12 +10,18 @@ import Comparator from '../../../utils/comparator/Comparator'; */ export default function binarySearch(sortedArray, seekElement, comparatorCallback) { + // Let's create comparator from the comparatorCallback function. + // Comparator object will give us common comparison methods like equal() and lessThen(). const comparator = new Comparator(comparatorCallback); + // These two indices will contain current array (sub-array) boundaries. let startIndex = 0; let endIndex = sortedArray.length - 1; + // Let's continue to split array until boundaries are collapsed + // and there is nothing to split anymore. while (startIndex <= endIndex) { + // Let's calculate the index of the middle element. const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2); // If we've found the element just return its position. @@ -33,5 +39,6 @@ export default function binarySearch(sortedArray, seekElement, comparatorCallbac } } + // Return -1 if we have not found anything. return -1; }