diff --git a/01_introduction_to_algorithms/ES6/01_binary_search.js b/01_introduction_to_algorithms/ES6/01_binary_search.js index 7b3e218..83704d6 100644 --- a/01_introduction_to_algorithms/ES6/01_binary_search.js +++ b/01_introduction_to_algorithms/ES6/01_binary_search.js @@ -1,3 +1,9 @@ +/** + * Searches recursively number from the list + * @param {Array} list Source array + * @param {number} item Search item + * @returns {(number|null)} Number if the value is found or NULL otherwise + */ const binarySearch = (list, item) => { let low = 0; let high = list.length - 1; @@ -8,8 +14,7 @@ const binarySearch = (list, item) => { if (guess === item) { return mid; - } - if (guess > item) { + } else if (guess > item) { high = mid - 1; } else { low = mid + 1; diff --git a/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js b/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js index d203777..fd96dad 100644 --- a/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js +++ b/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js @@ -1,36 +1,34 @@ /** * Searches recursively number from the list - * @param {Array} list + * @param {Array} list Source array * @param {number} item Search item * @param {number} low Lower limit of search in the list * @param {number} high Highest limit of search in the list - * @return {(number | null)} Number if the value is found or NULL otherwise + * @returns {(number|null)} Number if the value is found or NULL otherwise */ -const binarySearch = ( list, item, low = 0, high = list.length - 1 ) => { - let mid = Math.floor((low + high) / 2); - let guess = list[mid]; +const binarySearch = (list, item, low = 0, high = list.length - 1) => { + if (low > high) return null; - if ( low > high ) return null; + const mid = Math.floor((low + high) / 2); + const guess = list[mid]; - if ( guess === item ) { - return mid; - } else if ( guess > item ) { - high = mid - 1; - return binarySearch( list, item, low, high ); - } else { - low = mid + 1; - return binarySearch( list, item, low, high ); - } + if (guess === item) { + return mid; + } else if (guess > item) { + return binarySearch(list, item, low, mid - 1); + } else { + return binarySearch(list, item, mid + 1, high); + } }; /** * Creates the array that contains numbers 1...N * @param {number} n - number N - * @return {Array} + * @returns {Array} Array that contains numbers 1...N */ -const createArr = ( n ) => Array.from({length: n}, (v, k) => k + 1); +const createArr = n => Array.from({ length: n }, (v, k) => k + 1); -const myList = createArr( 100 ); +const myList = createArr(100); -console.log( binarySearch( myList, 3 ) ); // 2 -console.log( binarySearch( myList, -1 ) ); // null +console.log(binarySearch(myList, 3)); // 2 +console.log(binarySearch(myList, -1)); // null diff --git a/01_introduction_to_algorithms/javascript/01_binary_search.js b/01_introduction_to_algorithms/javascript/01_binary_search.js index 1d5cc26..a9c27f4 100644 --- a/01_introduction_to_algorithms/javascript/01_binary_search.js +++ b/01_introduction_to_algorithms/javascript/01_binary_search.js @@ -1,22 +1,28 @@ -'use strict'; +"use strict"; +/** + * Searches recursively number from the list + * @param {Array} list Source array + * @param {number} item Search item + * @returns {(number|null)} Number if the value is found or NULL otherwise + */ function binary_search(list, item) { let low = 0; let high = list.length - 1; - + while (low <= high) { - let mid = Math.floor((low + high) / 2); - let guess = list[mid]; -  if (guess === item) { + const mid = Math.floor((low + high) / 2); + const guess = list[mid]; + + if (guess === item) { return mid; - } - if (guess > item) { + } else if (guess > item) { high = mid - 1; } else { low = mid + 1; } } - + return null; }