From c6a36987c834acc998e2b04b480d85905d735dbe Mon Sep 17 00:00:00 2001 From: RitikDua Date: Thu, 1 Oct 2020 21:24:12 +0530 Subject: [PATCH 1/3] Added BinarySearchIterative Function * BinarySearchIterative Function is added * BinarySearch function is renamed to BinarySearchRecurisve * changes in test function - 3 binarySearchRecursive tests - 3 binarySearchIterative tests --- Search/BinarySearch.js | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 3a150d638..344b5d355 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -7,7 +7,7 @@ * value is found or the interval is empty. */ -function binarySearch (arr, x, low = 0, high = arr.length - 1) { +function binarySearchRecursive (arr, x, low = 0, high = arr.length - 1) { const mid = Math.floor(low + (high - low) / 2) if (high >= low) { @@ -18,10 +18,10 @@ function binarySearch (arr, x, low = 0, high = arr.length - 1) { if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid - return binarySearch(arr, x, low, mid - 1) + return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high - return binarySearch(arr, x, mid + 1, high) + return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item @@ -29,6 +29,29 @@ function binarySearch (arr, x, low = 0, high = arr.length - 1) { } } +function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { + + while (high >= low) { + const mid = Math.floor(low + (high - low) / 2) + + if (arr[mid] === x) { + // item found => return its index + return mid + } + + if (x < arr[mid]) { + // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid + high = mid - 1 + } else { + // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high + low = mid + 1 + } + } + // if low > high => we have searched the whole array without finding the item + return -1 +} + + /* ---------------------------------- Test ---------------------------------- */ const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] @@ -61,10 +84,10 @@ const stringArr = [ 'Zulu' ] -console.log(binarySearch(arr, 3)) -console.log(binarySearch(arr, 7)) -console.log(binarySearch(arr, 13)) +console.log(binarySearchRecursive(arr, 3)) +console.log(binarySearchIterative(arr, 7)) +console.log(binarySearchRecursive(arr, 13)) -console.log(binarySearch(stringArr, 'Charlie')) -console.log(binarySearch(stringArr, 'Zulu')) -console.log(binarySearch(stringArr, 'Sierra')) +console.log(binarySearchIterative(stringArr, 'Charlie')) +console.log(binarySearchRecursive(stringArr, 'Zulu')) +console.log(binarySearchIterative(stringArr, 'Sierra')) From b4648f7b935d732c6583e1cebc0b8e8c45e8ca46 Mon Sep 17 00:00:00 2001 From: RitikDua Date: Thu, 1 Oct 2020 21:30:54 +0530 Subject: [PATCH 2/3] Code Refacoring --- Search/BinarySearch.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 344b5d355..d17804fc6 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -28,9 +28,7 @@ function binarySearchRecursive (arr, x, low = 0, high = arr.length - 1) { return -1 } } - function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { - while (high >= low) { const mid = Math.floor(low + (high - low) / 2) @@ -51,7 +49,6 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { return -1 } - /* ---------------------------------- Test ---------------------------------- */ const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] From fdeda728054d910978ff69e97924d2358cd4da83 Mon Sep 17 00:00:00 2001 From: RitikDua Date: Thu, 1 Oct 2020 21:32:44 +0530 Subject: [PATCH 3/3] Code Refacoring --- Search/BinarySearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index d17804fc6..4d4789d65 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -46,7 +46,7 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { } } // if low > high => we have searched the whole array without finding the item - return -1 + return -1 } /* ---------------------------------- Test ---------------------------------- */