enh(JavaScript): binary-search (#645)

pull/652/head
atulll 2021-12-09 18:39:19 +05:30 committed by GitHub
parent b5d44d3bd3
commit 927da7a9ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View File

@ -2,12 +2,12 @@
/*
* Arguments to Function
* arr - array ( Sorted Only )
* low - lower index of array (0)
* high - max index of array (length of array - 1 )
* item = Element to be searched .
* item = Element to be searched
* low - lower index of array (0).
*/
function binaryRecursive(arr, low, high, item) {
function binaryRecursive(arr, high, item, low = 0) {
// Base Case for the termination of Recursion
if (low > high) {
@ -29,19 +29,19 @@ function binaryRecursive(arr, low, high, item) {
// greater than middle element and so item too.
// Make a recursive call to the left Half
return binaryRecursive(arr, low, mid - 1, item);
return binaryRecursive(arr, mid - 1, item, low);
} else {
// Item is greater than the middle Element
// Ignore the Left Half ,as left half contains element less
// than middle element and so item too .
// Make recursive call to the right Half
return binaryRecursive(arr, mid + 1, high, item);
return binaryRecursive(arr, high, item, mid + 1);
}
}
// returns 3 , Found at Index 3
console.log(binaryRecursive([1, 3, 5, 7, 8, 9], 0, 5, 7));
console.log(binaryRecursive([1, 3, 5, 7, 8, 9], 5, 7));
// returns -1 , 10 is not present in array
console.log(binaryRecursive([1, 3, 5, 7, 8, 9], 0, 5, 10));
console.log(binaryRecursive([1, 3, 5, 7, 8, 9], 5, 10));

View File

@ -5,7 +5,7 @@
function binarySearch(item, arr) {
let first = 0; // left endpoint or index of first array element
let last = arr.length - 1; // right endpoint or index of lat array element .
let last = arr.length - 1; // right endpoint or index of last array element .
while (first <= last) {
const middle = Math.floor((first + last) / 2);