From 1249afec6a3f0c122c54ea8d8994f78311e162ad Mon Sep 17 00:00:00 2001 From: AbmSourav Date: Wed, 16 Feb 2022 13:23:56 +0600 Subject: [PATCH 1/3] required, optional param left-to-right approch in BS --- Recursive/BinarySearch.js | 6 +++--- Recursive/test/BinarySearch.test.js | 10 +++++----- package.json | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Recursive/BinarySearch.js b/Recursive/BinarySearch.js index 6f6e216df1..0d0d690595 100644 --- a/Recursive/BinarySearch.js +++ b/Recursive/BinarySearch.js @@ -10,7 +10,7 @@ * @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm) */ -const binarySearch = (arr, low = 0, high = arr.length - 1, searchValue) => { +const binarySearch = (arr, searchValue, low = 0, high = arr.length - 1) => { if (high >= low) { const mid = low + Math.floor((high - low) / 2) @@ -22,11 +22,11 @@ const binarySearch = (arr, low = 0, high = arr.length - 1, searchValue) => { // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > searchValue) { - return binarySearch(arr, low, mid - 1, searchValue) + return binarySearch(arr, searchValue, low, mid - 1) } // Else the element can only be present in right subarray - return binarySearch(arr, mid + 1, high, searchValue) + return binarySearch(arr, searchValue, mid + 1, high) } // We reach here when element is not present in array diff --git a/Recursive/test/BinarySearch.test.js b/Recursive/test/BinarySearch.test.js index 2efc92c2e9..52574c3403 100644 --- a/Recursive/test/BinarySearch.test.js +++ b/Recursive/test/BinarySearch.test.js @@ -7,26 +7,26 @@ describe('BinarySearch', () => { it('should return index 3 for searchValue 10', () => { const searchValue = 10 - expect(binarySearch(arr, low, high, searchValue)).toBe(3) + expect(binarySearch(arr, searchValue, low, high)).toBe(3) }) it('should return index 0 for searchValue 2', () => { const searchValue = 2 - expect(binarySearch(arr, low, high, searchValue)).toBe(0) + expect(binarySearch(arr, searchValue, low, high)).toBe(0) }) it('should return index 13 for searchValue 999', () => { const searchValue = 999 - expect(binarySearch(arr, low, high, searchValue)).toBe(13) + expect(binarySearch(arr, searchValue, low, high)).toBe(13) }) it('should return -1 for searchValue 1', () => { const searchValue = 1 - expect(binarySearch(arr, low, high, searchValue)).toBe(-1) + expect(binarySearch(arr, searchValue, low, high)).toBe(-1) }) it('should return -1 for searchValue 1000', () => { const searchValue = 1000 - expect(binarySearch(arr, low, high, searchValue)).toBe(-1) + expect(binarySearch(arr, searchValue, low, high)).toBe(-1) }) }) diff --git a/package.json b/package.json index 8aa9f28219..9b27a9d4a4 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "main": "", "scripts": { "test": "jest --no-cache", + "binarySearch": "jest ./Recursive/test/BinarySearch.test.js", "style": "standard" }, "author": "TheAlgorithms", From c380a4f60f7deb38c41b7b598a7bac992a24c498 Mon Sep 17 00:00:00 2001 From: AbmSourav Date: Wed, 16 Feb 2022 13:28:28 +0600 Subject: [PATCH 2/3] base case return early to avoid nested if block --- Recursive/BinarySearch.js | 31 +++++++++++++++---------------- package.json | 1 - 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Recursive/BinarySearch.js b/Recursive/BinarySearch.js index 0d0d690595..b3f4e13f0e 100644 --- a/Recursive/BinarySearch.js +++ b/Recursive/BinarySearch.js @@ -11,26 +11,25 @@ */ const binarySearch = (arr, searchValue, low = 0, high = arr.length - 1) => { - if (high >= low) { - const mid = low + Math.floor((high - low) / 2) + // base case + if (high < low || arr.length == 0) return -1; + + const mid = low + Math.floor((high - low) / 2) - // If the element is present at the middle - if (arr[mid] === searchValue) { - return mid - } - - // If element is smaller than mid, then - // it can only be present in left subarray - if (arr[mid] > searchValue) { - return binarySearch(arr, searchValue, low, mid - 1) - } + // If the element is present at the middle + if (arr[mid] === searchValue) { + return mid + } - // Else the element can only be present in right subarray - return binarySearch(arr, searchValue, mid + 1, high) + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > searchValue) { + return binarySearch(arr, searchValue, low, mid - 1) } - // We reach here when element is not present in array - return -1 + // Else the element can only be present in right subarray + return binarySearch(arr, searchValue, mid + 1, high) + } export { binarySearch } diff --git a/package.json b/package.json index 9b27a9d4a4..8aa9f28219 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "main": "", "scripts": { "test": "jest --no-cache", - "binarySearch": "jest ./Recursive/test/BinarySearch.test.js", "style": "standard" }, "author": "TheAlgorithms", From 5fd9305c0fdad0e668308d1eb3fb67ceb54dac13 Mon Sep 17 00:00:00 2001 From: AbmSourav Date: Wed, 16 Feb 2022 14:47:55 +0600 Subject: [PATCH 3/3] codes formated with standard.js --- Recursive/BinarySearch.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Recursive/BinarySearch.js b/Recursive/BinarySearch.js index b3f4e13f0e..4c9a2d78a7 100644 --- a/Recursive/BinarySearch.js +++ b/Recursive/BinarySearch.js @@ -12,8 +12,8 @@ const binarySearch = (arr, searchValue, low = 0, high = arr.length - 1) => { // base case - if (high < low || arr.length == 0) return -1; - + if (high < low || arr.length === 0) return -1 + const mid = low + Math.floor((high - low) / 2) // If the element is present at the middle @@ -29,7 +29,6 @@ const binarySearch = (arr, searchValue, low = 0, high = arr.length - 1) => { // Else the element can only be present in right subarray return binarySearch(arr, searchValue, mid + 1, high) - } export { binarySearch }