Issues-126 - Fixed formatting, jsdoc and example for JavaScript/ES6 (#127)
* Issues-126 - Fixed formatting, jsdoc and non-working example for JavaScript/ES6 * Issues-126 - Fixed jsdoc
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user