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) => {
|
const binarySearch = (list, item) => {
|
||||||
let low = 0;
|
let low = 0;
|
||||||
let high = list.length - 1;
|
let high = list.length - 1;
|
||||||
@@ -8,8 +14,7 @@ const binarySearch = (list, item) => {
|
|||||||
|
|
||||||
if (guess === item) {
|
if (guess === item) {
|
||||||
return mid;
|
return mid;
|
||||||
}
|
} else if (guess > item) {
|
||||||
if (guess > item) {
|
|
||||||
high = mid - 1;
|
high = mid - 1;
|
||||||
} else {
|
} else {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
|
|||||||
@@ -1,36 +1,34 @@
|
|||||||
/**
|
/**
|
||||||
* Searches recursively number from the list
|
* Searches recursively number from the list
|
||||||
* @param {Array} list
|
* @param {Array} list Source array
|
||||||
* @param {number} item Search item
|
* @param {number} item Search item
|
||||||
* @param {number} low Lower limit of search in the list
|
* @param {number} low Lower limit of search in the list
|
||||||
* @param {number} high Highest 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 ) => {
|
const binarySearch = (list, item, low = 0, high = list.length - 1) => {
|
||||||
let mid = Math.floor((low + high) / 2);
|
if (low > high) return null;
|
||||||
let guess = list[mid];
|
|
||||||
|
|
||||||
if ( low > high ) return null;
|
const mid = Math.floor((low + high) / 2);
|
||||||
|
const guess = list[mid];
|
||||||
|
|
||||||
if ( guess === item ) {
|
if (guess === item) {
|
||||||
return mid;
|
return mid;
|
||||||
} else if ( guess > item ) {
|
} else if (guess > item) {
|
||||||
high = mid - 1;
|
return binarySearch(list, item, low, mid - 1);
|
||||||
return binarySearch( list, item, low, high );
|
} else {
|
||||||
} else {
|
return binarySearch(list, item, mid + 1, high);
|
||||||
low = mid + 1;
|
}
|
||||||
return binarySearch( list, item, low, high );
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the array that contains numbers 1...N
|
* Creates the array that contains numbers 1...N
|
||||||
* @param {number} n - number 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, 3)); // 2
|
||||||
console.log( binarySearch( myList, -1 ) ); // null
|
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) {
|
function binary_search(list, item) {
|
||||||
let low = 0;
|
let low = 0;
|
||||||
let high = list.length - 1;
|
let high = list.length - 1;
|
||||||
|
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
let mid = Math.floor((low + high) / 2);
|
const mid = Math.floor((low + high) / 2);
|
||||||
let guess = list[mid];
|
const guess = list[mid];
|
||||||
if (guess === item) {
|
|
||||||
|
if (guess === item) {
|
||||||
return mid;
|
return mid;
|
||||||
}
|
} else if (guess > item) {
|
||||||
if (guess > item) {
|
|
||||||
high = mid - 1;
|
high = mid - 1;
|
||||||
} else {
|
} else {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user