Added more implementations (JS) (#67)
* Added recursive binary search (JS) * Added recursive selection sorting * Added another loop implementation sum func by reduce (JS) * Recursion reduced by one iteration * Recursive binary search in ES6 added to appropriate folder * JS files ordered by standards (ES4/ES6) * Added hashtable implementation in JS * Fixed typo with LENGTH prop * Added Euclidian algorithm for two numbers and set of them * Added universal selection sort * Commented output * Added ES6 version of Euclidean algorithm * Converted from ES6 to ES5 * #69 Added search for LCS * #69 added levenstein algorithm * Removed excessive property * Removed excessive file * Removed excessive function calls * Removed excessive file * Removed excessive file * Renamed * Fixed indentation
This commit is contained in:
committed by
Aditya Bhargava
parent
a5fe9178dd
commit
0d5d0164ce
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Searches recursively number from the list
|
||||
* @param {Array} list
|
||||
* @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
|
||||
*/
|
||||
const binarySearch = ( list, item, low, high ) => {
|
||||
let arrLength = list.length;
|
||||
while ( low <= high ) {
|
||||
let mid = Math.floor((low + high) / 2);
|
||||
let guess = list[mid];
|
||||
|
||||
if ( guess === item ) {
|
||||
return mid;
|
||||
} else if ( guess > item ) {
|
||||
high = mid - 1;
|
||||
list = list.slice( 0, mid );
|
||||
return binarySearch( list, item, low, high );
|
||||
} else {
|
||||
low = mid + 1;
|
||||
list = list.slice( low, arrLength );
|
||||
return binarySearch( list, item, low, high );
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the array that contains numbers 1...N
|
||||
* @param {number} n - number N
|
||||
* @return {Array}
|
||||
*/
|
||||
const createArr = ( n ) => Array.from({length: n}, (v, k) => k + 1);
|
||||
|
||||
const myList = createArr( 100 );
|
||||
let low = 0;
|
||||
let high = myList.length - 1;
|
||||
|
||||
console.log( binarySearch( myList, 3, low, high ) ); // 2
|
||||
console.log( binarySearch( myList, -1, low, high ) ); // null
|
||||
Reference in New Issue
Block a user